2011-04-12 45 views
1

我想將這個sql轉換成我的LINQ查詢,並且有很多問題。使用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(); 

回答

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();