Order parent object as a child property in LINQ

How to arrange child objects in LINQ?

Classes A, B, and C. A has set B and B has set C. I want to order object A by the Ordinal (int) property for C.

var query = from a in db.A
        orderby a.Bs.OrderBy(x=> x.C.Ordinal)   <--- ??
        select a;

It seems like I cannot define an orderby statement for this.

EDIT:

Sorry, my original expression was incorrect:

A has a collection of B and B containing an object C. C is not a collection.

Each A must be ordered by the property of the C-ordinal.

OTHER CHANGE / Decision:

I ended up working with .OrderBy (b => bCOrdinal) on the client for each collection B in during display. It worked out anyway, since I can let the client order everything they need, instead of embedding it in my repository.

+3
source share
5

, .

- , ? 1 5 2 4, ?

+6

. . , .

+2

, C.Ordinal A.

IEnumerable<A> result = db.As
  .OrderBy(a => a.Bs
    .Max(b => b.C.Ordinal)
  );

: "b.Cs "

+1

, , , :

.OrderBy(b = > b.C.FirstOrDefault(). Ordinal)

+1

I ended up working with .OrderBy (b => bCOrdinal) on the client for each collection B in during display. It worked out anyway, since I can let the client order everything they need, instead of embedding it in my repository.

0
source

Source: https://habr.com/ru/post/1704585/


All Articles