如何按属性1分组IQueryable但按属性2分组? [英] How to group a IQueryable by property 1 but order by property 2?

查看:206
本文介绍了如何按属性1分组IQueryable但按属性2分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我之前的问题此处

我用过

var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy((License => License.dateCreated)).OrderByDescending(lics => lics.Key).First();

要将IQueryable分组

To group the IQueryable

allEvaluationLicenses 

使用许可证的属性1(即"dateCreated")

by using License's property 1 which is 'dateCreated'

但是,现在,如何使用不同的属性(例如'nLicenceID')对它们进行排序?

But now, how can I order them by using a different property such as 'nLicenceID'?

是否可以执行以下操作:

Is it possible to do something like this:

var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy((License => License.dateCreated)).OrderByDescending(lics => (sort by nLicenseID here) ).First();

推荐答案

对于LINQ-to-Objects,每个组中 inside 中的对象保留发现它们的顺序:

For LINQ-to-Objects, the objects inside each group retain the ordering in which they are discovered:

IGrouping<TKey, TElement>对象的生成顺序是基于产生每个IGrouping<TKey, TElement>的第一个键的源中元素的顺序.分组中的元素按它们在源中出现的顺序产生.

The IGrouping<TKey, TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey, TElement>. Elements in a grouping are yielded in the order they appear in source.

因此:如果您的目的是订购每个组的内容,只需订购来源:

So: if your aim is to order the contents of each group, simply order the source:

var distinctAllEvaluationLicenses = allEvaluationLicenses
    .OrderByDescending({whatever})
    .GroupBy({etc}).First(); 

请注意,这不能保证可用于其他LINQ来源,并且请注意,这不会影响的显示顺序.为此,您也许可以执行以下操作:

Note that this is not guaranteed to work for other LINQ sources, and note that it doesn't influence the order in which the groups are presented. To do that you could perhaps do something like:

var distinctAllEvaluationLicenses = allEvaluationLicenses
    .GroupBy({etc}).
    .OrderBy(grp => grp.Min(item => x.SomeProp)).First(); 

,将按照每个组中最小SomeProp的顺序显示个组.显然,可以根据需要将max/etc进行调整.

which would present the groups in order of the minimum SomeProp in each. Obviously adjust to max / etc as necessary.

这篇关于如何按属性1分组IQueryable但按属性2分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆