The difference between the extension method and direct query

var selectedProducts = from p in products where p.Category == 1 select p; var selectedProducts = products.Where(p=>p.Category==1) ; 

The above 2 statements seem to give the same result.

Then what is the difference (several times inside)?

Which one is more effective?

+4
source share
2 answers

There is no difference. The first (query expression) is translated into the second by the compiler and does not affect the execution time.

See also:

+6
source

In this case, there is no difference between the two methods, but in some cases it is better to use the request and in some cases it is better to use the extension method or it is impossible to use the request.

you can use the query in a situation that is complicated by extension methods and unreadable, for example Join. You can also use the extension method in some cases, for example, Distinct, which is not available in the query syntax. You can also use extension method calls to use method chaining to improve code readability.

You can use a combination of extension method and query, but not very well (code readability): for example

 (from p in products where p.Category == 1 select p).Distinct() 
+2
source

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


All Articles