Using multiple Where () or && calls for LinqToEntities queries

Are the following two queries equivalent? If they are not equivalent, which is better? Is there a way to see the sql output of queries?

var query1 = items.Where(i => i.Enabled == true).Where(i => i.Name == "Bob");

var query2 = items.Where(i => i.Enabled == true && i.Name == "Bob");
+3
source share
2 answers

As Andrew says, these two options are equivalent. One practical difference is that you can easily create conditions in a Wherereservation programmatically. For example, if you want to exclude certain names:

var query = items;
for(string name in excluded) 
  query = query.Where(i => i.Name != excluded); 

This is something that is not easy to do when writing a query using an operator &&.

+3
source

SQL - , LinqPad, , ( ). LINQ, T-SQL, , , . , , .

+2

Source: https://habr.com/ru/post/1768454/


All Articles