How the LINQ extension chain disconnects from other extensions

I learned C # back in 2006 and recently tried to get back to it. I found out that they have added something called LINQ Extensions to C # 3.0. Now I'm familiar with extension methods, and I'm just thinking about the features of those related to IEnumerables.

Earlier today, I and one of my colleagues discussed whether the following blocks of code are equivalent:

List<int> integers;
IEnumerable<int> subResult = items.Where(i => IsPrime(i));
IEnumerable<int> orderedResult = subResult.OrderBy(i => i);

vs

List<int> integers;
IEnumerable<int> result = items.Where(i => IsPrime(i)).OrderBy(i => i);

He told me that the latter was more efficient because the extension used a late request from its source. I'm not quite sure that I understood what he meant, and I was wondering if he was right.

+3
source share
4 answers

. / .

, .Where, , , . GetEnumerator .Where , .Where .

.OrderBy. , .GetEnumerator.

, . , , , , . . , . , , , .

Jon Skeet LINQ.

https://msmvps.com/blogs/jon_skeet/archive/2008/02/20/visual-linq-watch-query-expressions-as-they-happen.aspx

+5

.

.
, subResult - , IL release.

+3

LINQ IEnumerable. LINQ . , . , , .

, , , LINQ (Where OrderBy) , , , .

+1

- .

:

var result = System.Linq.Enumerable.Where(
   myList, a => a.CustomerName == "Bob");

Using extension methods, myList has a Where method (this is not the case).

var result = myList.Where(a => a.CustomerName == "Bob");
0
source

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


All Articles