Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include sorted navigation properties with Entity Framework [duplicate]

I have an entity A with a collection of B inside. I load them with a _entity.A.Include(a => a.B)

Now I want to have the B's into A sorted by a custom OrderBy. I tried _entity.A.Include(a => a.B.OrderBy(o => o.Version) but I get a :

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

Any ideas on how to accomplish this?

Thanks.

Version is an integer.

like image 496
Ignacio Soler Garcia Avatar asked Nov 05 '12 14:11

Ignacio Soler Garcia


1 Answers

I think in this case you can try:

var list = _entity.A.Include("B").ToList();
list.ForEach(m => m.B = m.B.OrderBy(o => o.Version));

or:

_entity.A.Include("B").Select(m => new A {
        //some props,
        B = m.B.OrderBy(o => o.Version)
        });
like image 129
Mateusz Rogulski Avatar answered Oct 16 '22 22:10

Mateusz Rogulski