LINQ GROUP BY和MAX()

时间:2012-04-10 09:11:11

标签: c# sql linq group-by max

我正在试图找出如何在LINQ中编写SQL语句,但目前我找不到办法,这是SQL命令:

SELECT cs.Site_Name, MAX(ed.EffectiveDate_Date)
FROM [WAPMaster].[Factsheets].[EffectiveDate] ed,
[WAPMaster].[Configuration].[Site] cs
WHERE cs.Site_Id = ed.EffectiveDate_SiteId
GROUP BY cs.Site_Name

有人可以帮我解决linq语法吗?

**到目前为止我正在尝试这个(感谢levanlevi)

var test = (from e in this._wapDatabase.EffectiveDates
            join c in this._wapDatabase.Sites 
            on c.Site_Id equals e.EffectiveDate_SiteId
            group e by c.Site_Name into r
            select new
            {
                r.Key.SiteName,
                EffectiveDate = r.Max(d => d.EffectiveDate_Date)
            }); 

但我收到以下错误:

http://i.stack.imgur.com/AkJ5V.png

2 个答案:

答案 0 :(得分:10)

SELECT  cs.Site_Name ,
        MAX(ed.EffectiveDate_Date)
FROM    [WAPMaster].[Factsheets].[EffectiveDate] ed ,
        [WAPMaster].[Configuration].[Site] cs
WHERE   cs.Site_Id = ed.EffectiveDate_SiteId
GROUP BY cs.Site_Name



from e in WAPMaster.Factsheets.EffectiveDate
join c in WAPMaster.Configuration.Site
on c.Site_Id equals e.EffectiveDate_SiteId
group e by c.Site_Name into r
select new { SiteName = r.Key, EffectiveDate = r.Max(d=>d.EffectiveDate_Date)}

答案 1 :(得分:1)

var test = (from effectiveDates in this._wapDatabase.EffectiveDates                         
            from sites in this._wapDatabase.Sites                         
            where sites.Site_Id = effectiveDates.EffectiveDate_SiteId
                     group effectiveDates by sites.Site_Id into g                         
             select new {  siteId = g.key , effectiveDate = g.max(ed => ed.EffectiveDate_Date)});