I got the code below from here
var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy((License =>
License.dateCreated)).Select(License => License.First());
How can I select the latest 'dateCreated' instead of the First one?
If all you want is the max dateCreated
, try this:
var results = allEvaluationLicenses.Max(x => x.dateCreated);
If you want the licenses with the max dateCreated
, try this:
var results =
allEvaluationLicenses.GroupBy(x => x.dateCreated)
.OrderByDescending(g => g.Key)
.First();
Or in query syntax:
var results =
(from l in allEvaluationLicenses
group l by l.dateCreated into g
orderby g.Key descending
select g)
.First();
You can use Max
to get the largest of a sequence.
var distinctAllEvaluationLicenses = allEvaluationLicenses.GroupBy(License =>
License.dateCreated)
.Max(group => group.Key);
That said, in this particular context, there doesn't seem to be any reason to do the grouping at all:
var distinctAllEvaluationLicenses = allEvaluationLicenses
.Max(License=> License.dateCreated)