0

are the following two linq query scenario same , performance wise ?

First :

    var result= ._context.PDUTeams
                        .Where(x=>x.Name=="Steve");
    if(some condition)
    {
    result=result.Where(x=>x.Age==23);  // people having age=23 are 2
    }
    else{
     result=result.Where(x=>x.Age==19); // people having age=19 are 2
     }
    var data= await result.toList();

second :

var result= ._context.PDUTeams
                    .Where(x=>x.Name=="Steve").Where(x=>x.Age==19);


var data=await result.tolist();

Does the async operator fetches the result or the data will be fetched after excuting the first linq statement.

7
  • 1
    Your code doesn't make sense. If some condition is false, then result will be empty, no matter what PDUTeams contains Commented May 25, 2017 at 11:08
  • what if all the codition satisfies! Commented May 25, 2017 at 11:09
  • see i mean to say that if i am applying the where clause in linq at once and applying where clause with if - else statements , does it have any performance issue ? Commented May 25, 2017 at 11:10
  • Performance is irrelevant here because the queries are different.In the first case, you execute some condition && condition or some condition && another condition. In the second, you execute only some condition && another condition. In both cases it's a single query that will generate a similar SQL statement Commented May 25, 2017 at 11:22
  • Why are you asking about performance at all? If you have performance issues you should look at the generated query and any missing indexes. Using multiple Where statements may result in slightly more complex queries than conditionA && conditionB although SQL Server is smart enough to generate the same execution plan Commented May 25, 2017 at 11:25

1 Answer 1

1

The data is fetched by the ToList() (with or without the await). Before that you are just building a query.

Two queries which are identical by the time they reach the ToList(), will generate identical SQL code and will run the same.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.