日期:2014-05-20  浏览次数:20825 次

小弟刚学linq一条简单的sql语句如何转为linq
SQL code

SELECT c.SiteName,max(d.ScheduleTime),a.EnterpriseName,a.EnterpriseInfoID FROM EnterpriseInfo AS a 
INNER JOIN EnterpriseSite AS b ON b.EnterpriseInfoID = a.EnterpriseInfoID
INNER JOIN SiteInfo AS c ON c.SiteInfoID = b.SiteInfoID 
INNER JOIN ScheduleInfo AS d ON d.EnterpriseSiteID = b.EnterpriseSiteID 
INNER JOIN EventType AS e ON e.EventTypeID = d.EventTypeID    
GROUP BY c.SiteName,a.EnterpriseName,a.EnterpriseInfoID



------解决方案--------------------
C# code

//代码手打,提供思路参考
var query=from a in EnterpriseInfo 
          join b in EnterpriseSite on  a.EnterpriseInfoID equals b.EnterpriseInfoID 
          join c in SiteInfo  on  b.SiteInfoID  equals c.SiteInfoID 
          join d in ScheduleInfo on b.EnterpriseSiteID  equals d.EnterpriseSiteID 
          join e in EventType on d.EventTypeID equals e.EventTypeID
          group new{a,b,c,d} by new {c.SiteName,a.EnterpriseName,a.EnterpriseInfoID} into g
          select new{g.Key.SiteName,ScheduleTime=g.Max(d=>ScheduleTime),g.Key.EnterpriseName,g.Key.EnterpriseInfoID};