Breaking sql statement in Linq in 2 or more parts depends on program condition

I am the founder of Linq, so I need help.

I don't know if there is Linq syntax, splitting the query into two or more parts,

as in the following example,

will the records be immediately downloaded from the sql server at every step or will they be sent to the server the moment I'll start viewing all the data? for example when I bind some objects (like Datagrid)

System.Linq.IQueryable<Panorami> Result = db.Panorami;
byte FoundOneContion = 0;

//step 1
if (!string.IsNullOrEmpty(Title))
{
  Result = Result.Where(p => SqlMethods.Like(p.Title, "%" + Title + "%"));

  FoundOneContion = 1;
}
//step 2
if (!string.IsNullOrEmpty(Subject))
{
  Result = Result.Where(p => SqlMethods.Like(p.Subject, "%" + Subject + "%"));
  FoundOneContion = 1;
}
if (FoundOneContion == 0)
{
  return null;
}
else
{
  return Result.OrderBy(p => p.Title).Skip(PS * CP).Take(PS);
}

      

If unfortunately Linq will load all records at once

(Therefore, such a doubt was correct for me!)

is there any syntax to solve the problem?

For example: ternary operator (condition? True part: false part)

For any suggestions I would really appreciate them. thanks everyone!

+1


a source to share


1 answer


The above method does not list the request, so no database calls are made. The request was constructed and failed.



You can enumerate the request by calling foreach

, or calling the method that calls foreach

(for example ToList

, ToArray

), or by calling GetEnumerator()

. This will execute the request.

+1


a source







All Articles