在 LINQ 中使用 Max 函数

发布于 2024-10-31 19:53:56 字数 773 浏览 1 评论 0原文

我正在尝试将此 sql 转换为我的 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();

I am trying to convert this sql into my LINQ query and am having alot of problems.

select Heading,CreatedBy,ReleaseID, MAX(PublishDate)
from Releases
group by Heading,CreatedBy,ReleaseID

I need to use this query in this LINQ format

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2024-11-07 19:53:56

像这样的东西:

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

Something like this:

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(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文