0

from what I've read, I can use LINQ to first group, then order each Group by using "SelectMany", which is described here: How to group a IQueryable by property 1 but order by property 2?

But this doesn't work for IQueryable I guess.

We basically get a BusinessObject with an Main-Entity and an IEnumable of Entities, so I'd like to first order by the Main-Entity sequence, then by each Name of the Subentities.

So I guess my query would look like this in LINQ to objects:

            var qry = GetQueryFromSomeWhere();
            qry = qry.OrderBy(f => f.MainEntity.SequenceNumber)
                .ThenBy(f => f.SubEntities.SelectMany(f => f.Name)); 

I could order this Names in the Query-Service, but it should be up the consumer to order the entities as he needs.

Is there a possibility to make this work kindahow without loading all Entities in the Memory?

Community
  • 1
  • 1
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • What is GetQueryFromSomeWhere() returns? I think to get what you want it should return List – Yuri Jun 09 '15 at 13:57
  • No, the Service returns an IQueryable on purpose with some "general" businessobjects, which the client can use to filter, sort etc. – Matthias Müller Jun 10 '15 at 09:54

1 Answers1

1

If I'am correctly understanding you want to sort records inside each group by record Name. I think that you could accomplish this by ordering records before doing a group by, try this code:

var q = from m in MainEntities
join s in SubEntities on m.Id equals s.MainId
orderby m.SequenceNumber, s.Name
group new { m, s } by m into grp
orderby grp.Key.SequenceNumber
select grp;
Oleg
  • 1,378
  • 11
  • 22
  • The GetQueryFromSomeWhere() delivers an already grouped IQueryable, as written I could sort already there, but this would hide some logic I'd not like to. But according to you answer, this is the only way, so I guess have to do it and comment the function proper. – Matthias Müller Jun 10 '15 at 07:12
  • I wonder whether this would actually work with all SQL Providers - does SQL guarantee that an `GROUP BY` will preserve a previous `ORDER BY`? – NetMage Mar 16 '18 at 19:29