[英]Linq query with grouping and Max date
class ProductVersion
{
string ProductId
string VersionNumber
VersionType versionType
DateTime ReleaseDate
}
因此,我需要从ProductVersions集合中获取列表,该列表具有按ProductId和VersionType分组的最新ReleaseDate。
为该组使用匿名类型:
var prodVersionList = AllProductVersions
.GroupBy(pv => new { pv.ProductId, pv.versionType })
.Select(g => g.OrderByDescending(pv => pv.ReleaseDate).First())
.ToList();
您必须将ProductVersion
的属性和字段设为public
,否则无法访问它们。
var result = productVersions
.GroupBy(x => new { x.ProductId, x.versionType })
.Select(x => new { x.Key.ProductId, x.Key.versionType, LatestReleaseDate = x.Max(y => y.ReleaseDate) })
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.