Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EF have ordered collections? The equivalent of list in the NHibernate Bag Vs Set Vs List world

Does the Entity Framework 4 have ordered collections?

For example my Order has a property that is a collection of OrderItems, but the order is important and I would rather not sort them before I access them.

See Nhibernate for an example: List vs Set vs Bag in NHibernate

like image 240
Adam Gordon Bell Avatar asked Aug 30 '10 01:08

Adam Gordon Bell


1 Answers

It doesn't. You can force an order in a couple of ways however :

This answer explains one EF4 LINQ Ordering Parent and all child collections with Eager Loading (.Include()) (kind of)

More simply, if you are querying a single order, you can do so with the orderlines ordered in a specifc way:

var thing = _repo.GetOrder(id)
 .Select(item => 
    new { item, ord = item.orderlines.OrderBy(o => o.orderbythis) }
    ).FirstOrDefault().item;
like image 106
Andiih Avatar answered Oct 04 '22 17:10

Andiih