如何从IQueryable.GroupBy中选择最新日期? [英] how to select the latest date from a IQueryable.GroupBy?

查看:197
本文介绍了如何从IQueryable.GroupBy中选择最新日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从此处

    var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy((License => 
License.dateCreated)).Select(License => License.First());

如何选择最新的"dateCreated"而不是第一个"?

How can I select the latest 'dateCreated' instead of the First one?

推荐答案

如果您想要的只是最大值dateCreated,请尝试以下操作:

If all you want is the max dateCreated, try this:

var results = allEvaluationLicenses.Max(x => x.dateCreated);

如果要使用最大dateCreated的许可证,请尝试以下操作:

If you want the licenses with the max dateCreated, try this:

var results =
    allEvaluationLicenses.GroupBy(x => x.dateCreated)
                         .OrderByDescending(g => g.Key)
                         .First();

或使用查询语法:

var results =
    (from l in allEvaluationLicenses
     group l by l.dateCreated into g
     orderby g.Key descending
     select g)
    .First();

这篇关于如何从IQueryable.GroupBy中选择最新日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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