C# LINQ GROUP BY 和 MAX()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10085947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:23:55 来源:igfitidea点击:
LINQ GROUP BY and MAX()
提问by Remo H. Jansen
I'm trying to find out how to write an SQL sentence in LINQ but I can't find a way to do it for the moment, this is the SQL command:
我试图找出如何在 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
Can someone help me witht he linq syntax please?
有人可以帮我使用 linq 语法吗?
**I'm trying this so far (thanks levanlevi)
**到目前为止我正在尝试这个(感谢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'm getting the following error:
但我收到以下错误:
采纳答案by levi
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)}
回答by userGS
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)});

