简体   繁体   English

LINQ to Entities:分组然后排序

[英]LINQ to Entities: Group then Order By

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? 从我所读的内容中,我可以使用LINQ来对第一个组进行分组,然后使用“ SelectMany”对每个组进行排序,如下所述: 如何按属性1分组IQueryable但按属性2分组?

But this doesn't work for IQueryable I guess. 但这对IQueryable不起作用。

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. 我们基本上得到了一个带有主实体和IEnumable实体的BusinessObject,所以我想先按主实体序列排序,然后再按子实体的每个名称排序。

So I guess my query would look like this in LINQ to objects: 所以我想我的查询在LINQ中看起来像这样:

            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. 我可以在Query-Service中订购此名称,但是消费者应该根据需要订购实体。

Is there a possibility to make this work kindahow without loading all Entities in the Memory? 是否有可能在不将所有实体加载到内存的情况下使这种工作正常进行?

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM