C# linq include before-after where

后端 未结 1 1166
逝去的感伤
逝去的感伤 2020-12-16 12:04

In linq is there a difference between:

EFDbContext _db = new EFDbContext();



  1)_db.UserQuizes
        .Where(uq => uq.UserId == currentUserId &&am         


        
相关标签:
1条回答
  • 2020-12-16 12:30

    Ordering of instructions like you've shown often won't make a difference in EF or LINQ to SQL. The query builder turns your entire LINQ statement into an abstract logical representation, and then another pass converts the logical structure into a SQL statement. So the WHERE predicates are all going to end up in the same place. The predicates inside a First() just get pushed over to the WHERE clause. The Include statements also get accumulated and projected to JOINs to include the extra columns needed to produce the included entity.

    So the short answer is that EF will usually produce the most logical SQL statement regardless of the order in which you constructed your LINQ statement. If you need to tune it further, you should look at a stored procedure where you can hand-craft the SQL.

    0 讨论(0)
提交回复
热议问题