开发者

Linq - flatten and sort by parent

Simple question, not sure what the best answer is although I suspect it involves Linq.

I have a list of "order" objects, each of which has an Id and a collection of "orderItem" objects, each of which in turn has an Id. However, t开发者_如何学Che orderItem objects don't contain a direct reference to the "order" ID.

I need to get a list of all the "orderItem" objects of all the "orders" in the list, sorted first by orderItemID and then orderID.

(If I had a reference to orderID in orderItemID it would be very easy but I don't, and it'd be ugly and inefficient to add one just to do this).

I can easily get the list of all the orderItem objects by iteration, obviously, but then I can't do the sorting I need. Thoughts?


That's nice and easy:

var query = from order in orders
            from item in order.OrderItems
            orderby item.OrderItemId, order.OrderId
            select item;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜