Does LINQ To Object do internal tuning?

I wonder how LINQ to Objects determines the best way to query a collection?

I mean, is there any difference (in how LINQ works inside) between the following LINQ queries:

var lst1 = ListOfComplexClass.Where(p => p.StrValue == "Whatever")
                             .Select(p => p.StrValue);

and

var lst2 = ListOfComplexClass.Select(p => p.StrValue)
                             .Where(p => p == "Whatever");

For example, will the first filter collect ListOfComplexClassand then get the property StrValue? Or will he do some tuning and first do Selectand then filter the returned collection?

+3
source share
5 answers

For example .. will the first filter collect ListOfComplexClass and then get the StrValue property? Or can it do some, perhaps settings, and make a selection first and then filter the returned collection?

( , p => p.StrValue Select ).

, , .

, .

var lst1 = ListOfComplexClass.Where(p => p.StrValue == "Whatever")
                             .Select(p => p.StrValue);
var e = lst1.GetEnumerator();

e.MoveNext(), , Select MoveNext() ListOfComplexClass.Where(p => p.StrValue == "Whatever"), MoveNext() ListOfComplexClass, p p.StrValue == "Whatever". p e.Current.

. , .

var lst2 = ListOfComplexClass.Select(p => p.StrValue)
                             .Where(p => p == "Whatever");
var e = lst2.GetEnumerator();

e.MoveNext(), Where MoveNext() ListOfComplexClass.Select(p => p.StrValue), p p == "Whatever". , MoveNext() ListOfComplexClass.Select(p => p.StrValue) MoveNext() ListOfComplexClass Current .

, . .

var suits = deck.Where(c => c.Suit == Suit.Diamond || c.Suit == Suit.Heart)
                .Select(c => c.Suit)

var suits = deck.Select(c => c.Suit)
                .Where(c => c == Suit.Diamond || c == Suit.Heart);

, . .

Select Where .

Where deck .

* Where .

, Select Select .

, Where *.

.

Where Select .

Select deck .

deck .

Select .

* Where Select.

, Where .

, Where Select *.

+2

, LINQ to Object ?

, . "". . , , .

, Linq-to-SQL Linq-to-Entities (IQueryable) - , Linq-to-Objects.

+2

. , , . , ( ), . , LINQ , .

0

Linq linq sql, - linq . - from .. in .. where .. select .. , . where (, ), where select , , โ€‹โ€‹, . , - , , , .

0
source

In case you are present, there is no difference.

But if you put a ToList in a ToArray somewhere out there, that might make a difference.

LINQ to Objects, like LINQ to SQL, still uses lazy execution, although it just works in memory objects.

This is because LINQ extensions in IEnumerable use expression trees. This means that LINQ expressions are executed only when necessary.

usually, if you bind IEnumerable extensions, this will not change the situation. however, if you need to change the types by half or call something like ToList, this will change the order of execution.

0
source

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


All Articles