1

我从这里得到了下面的代码

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

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

4

2 回答 2

3

如果你想要的只是 max dateCreated,试试这个:

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

如果您想要具有 max 的许可证dateCreated,请尝试以下操作:

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();
于 2013-04-29T16:15:48.810 回答
1

您可以使用Max来获得最大的序列。

var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy(License => 
License.dateCreated)
    .Max(group => group.Key);

也就是说,在这种特殊情况下,似乎根本没有任何理由进行分组:

var distinctAllEvaluationLicenses = allEvaluationLicenses
    .Max(License=> License.dateCreated)
于 2013-04-29T16:14:21.333 回答