Trying to use Linq with some Entity objects to do a crafty query here. Hoping for some help with a point I'm having a hard time finding decent documentation on:
Basically, I'm trying to use OrderBy to order against the child of an object I'm querying against. The difficult part is that the object has multiple children, and based on the object's type, I need to use one set of a children or another set of children to order by.
To clarify:
A can come in two types: i, or ii
If A is of type i, then I need to order by D: i.e, A has a B, which has many Cs, which has many Ds.
If A is of type ii, then I need to order by F: ie. A has an E, which has many Fs.
So the question is, how can I order by D and F, from A?
I'm hoping for something like:
IQueryable<AObject> aObj = query.OrderBy(aObject=> aObject.Type==i? aObject.B.C.D : aObject.E.F).Skip(offset).Take(limit).AsQueryable();
Of course, I'm also just confused开发者_JAVA百科 as to how to order the D's, when C has a collection of Ds
Thoughts? And thanks in advance!
You need to use an aggregate function such as Min or Max to pick a value that represents the collection and use that value for the ordering. For example:
IQueryable<AObject> aObj = query.OrderBy(aObject =>
aObject.Type==i ?
aObject.B.Cs.Max(c => c.Ds.Max(d => d.Foo)) :
aObject.E.Fs.Max(f => f.Bar)
).Skip(offset).Take(limit).AsQueryable();
精彩评论