data
.GroupBy(
x => x.CardId,
(x, y) => new {
Key = x,
Value = y.OrderByDescending(z => z.DateTimeStamp).FirstOrDefault()
}
);
This will group all the elements by CardId
, then order the elements of each group by DateTimeStamp
(descending), then pare down each group to only contain the first element. Finally, it returns an enumerable of "groups" (with the scare quotes since they're actually an anonymous type instead of an IGrouping
) where each group has the one item you're seeking.