c# - Does LINQ know how to optimize "queries"? -


suppose

 var ordered = mylist.orderby(x => x.prop1).thenby(x => x.prop2);  

does mylist.orderby(x => x.prop1) return filtered list, , further filter list thenby(x => x.prop2)? in other words, equivalent

var orderedbyprop1 = mylist.orderby(x => x.prop1); var ordered = orderedbyprop1.orderby(x => x.prop2); 

???

because it's possible optimize running sorting algorithm comparator:

var ordered = mylist.sort( (x,y) => x.prop1 != y.prop1 ? x.prop1 < y.prop1 : ( x.prop2 < y.prop2 ) ); 

if sort of optimization , intermediate lists not returned in process, how know how that? how write class optimizes chains of methods on itself? makes no sense.

does mylist.orderby(x => x.prop1) return filtered list

no. linq methods (at least typically) return queries, not results of executing queries.

orderby returns object which, when ask item, return first item in collection given particular ordering. until ask result it's not doing anything.

note can decent idea what's going on looking @ orderby returns. returns iorderedenumerable<t>. interface has method createorderedenumerable which:

performs subsequent ordering on elements of iorderedenumerable according key.

that method thenby uses indicate there subsequent ordering.

this means you're building of comparers want used, orderby , thenby calls before ever need generate single item in result set.

for more specifics on exactly how can go creating behavior, see jon skeet's blog series on subject.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -