在LINQ中使用Max函数

时间:2011-04-12 16:04:44

标签: linq

我正在尝试将此sql转换为我的LINQ查询,并且遇到了很多问题。

select Heading,CreatedBy,ReleaseID, MAX(PublishDate)
from Releases
group by Heading,CreatedBy,ReleaseID

我需要以这种LINQ格式使用此查询

  List<RelData> cmsPress = (from Release in dataContext.Releases
                                        select new RelData
                                        {

                                            Heading = Release.Heading,
                                            ReleaseID = Release.ReleaseID,
                                            CreatedBy = Release.CreatedBy,
                                            PublishDate =   Release.PublishDate.ToShortDateString()
                                        }).ToList();

1 个答案:

答案 0 :(得分:3)

这样的事情:

List<RelData> cmsPress = (from Release in dataContext.Releases
    group Release by new { Release.Heading, Release.ReleaseID, Release.CreatedBy } into g
    select new RelData
    {                                              
        Heading = g.Key.Heading,
        ReleaseID = g.Key.ReleaseID,
        CreatedBy = g.Key.CreatedBy,
        PublishDate = g.Max(x => x.PublishDate).ToShortDateString()
    }).ToList();