Need help using expression tree to create LINQ query

I need to create an epression tree for a LINQ query that looks something like this:

collection.OrderBy(e => ((MyObject)e["PropertyIndex"]).dictionary.Where(k => k.Key == "keyName").Select(k => k.Value).Single());

I looked at this link which explains how the OrderBy method chains are. I do not know how to add Where in OrderBy using the expression tree.

Update:

I need to sort data in memory dynamically. So the linq query might look something like this:

collection.OrederBy(field1).ThenByDescending(field2).ThenBy(field3)

I know only at runtime how many fields I need to sort. Any of the fields X can be a complex object. The type of the object will of course be known at runtime. One of the objects has a structure similar to that given above in the LINQ query. It has a dictionary, and I have to sort for a specific key. In my case, the dictionary contains localized data, such as:

{{"en-US", "word (en)"}, {"es-ES", "word (es)"} , ....}

I need to sort by a specific language.

+3
source share
2 answers

Your request seems to do this:

from k in collection
where k.Key == "keyName"
orderby ((MyObject)k)["PropertyIndex"]
select k.Value

and you could add additional sentences, for example:

from k in collection
where k.Key == "keyName"
&& k.OtherProperty == OtherValue
orderby ((MyObject)k)["PropertyIndex"]
select k.Value

. ( , ), .OrderBy(). , , , ():

.OrderBy( e => e.Property1 ).OrderBy( e => e.Property2 )

"" , :

var query = (from k in collection select k);
query = query.Where( e => e.Property1 == "value" );
var orderedQuery = query.OrderBy( e => e.Property1 );
orderedQuery = query.Orderby( e => e.Property2 );
var result = orderedQuery.Select( e => e.Value ).Single();

, . , IQueriable<T>, orderQuery IOrderedQueriable<T>, ( ) var.

+1

OrderBy ThenBy. IOrderedEnumerable.

, Where BEFORE any Order.

, , Dynamic LinQ .

0

All Articles