LINQ GROUP BY and MAX ()

I'm trying to figure out how to write a SQL clause in LINQ, but I can't find a way to do it yet, this is the SQL command:

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

      

Can someone help me with linq syntax please?

** I am trying this so far (thanks to 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)
            }); 

      

But I am getting the following error:

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

+3


source to share


2 answers


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)}

      



+10


source


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

      



+1


source







All Articles