简体   繁体   English

LINQ 到实体最大 select 按日期 EF Core 3 的最大项目

[英]LINQ to entites max select max item by date EF Core 3

How to select in select new statement Post that has max date?如何在select new声明Post中的 select 有最大日期?

var result = from category in dataRepository.Categories
                 from forum in category.Forums
                 from topic in forum.Topics
                 from post in topic.Posts
                 group new { category, post, post.DateTime } by new { category.Name, ForumName = forum.Name }
                 into resultSet
                 select new
                 {
                     TopicId = resultSet.Key.Name,
                     ForumName = resultSet.Key.ForumName,
                     Replies = resultSet.Count(),
                     MaxPostDate = resultSet.Max(t => t.DateTime),
                     Post = /*How to select here Post item max by date?*/
                 };

I tried我试过

        var result = from category in dataRepository.Categories
                     from forum in category.Forums
                     from topic in forum.Topics
                     from post in topic.Posts
                     group new { category, post } by new { category.Name, ForumName = forum.Name }
                     into resultSet
                     select new
                     {
                         TopicId = resultSet.Key.Name,
                         ForumName = resultSet.Key.ForumName,
                         Replies = resultSet.Count(),
                         LatestPost = resultSet.Where(t => t.post.DateTime == resultSet.Max(date => date.post.DateTime)).FirstOrDefault()
                     };

Getting error出现错误

.Max(date => date.post.DateTime))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Second try第二次尝试

        var result = from category in dataRepository.Categories
                     from forum in category.Forums
                     from topic in forum.Topics
                     from post in topic.Posts
                     group new { category, post, post.DateTime } by new { category.Name, ForumName = forum.Name }
                     into resultSet
                     select new
                     {
                         TopicId = resultSet.Key.Name,
                         ForumName = resultSet.Key.ForumName,
                         Replies = resultSet.Count(),
                         Post = resultSet.OrderByDescending(i => i.DateTime).Select(i => i).FirstOrDefault().post
                     };

Error错误

.OrderByDescending(i => i.DateTime)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  Post = resultSet.OrderByDescending(i=>i.DateTime).Select(i=>i).FirstOrDefault().post

See also: LINQ to SQL: GroupBy() and Max() to get the object with latest date另请参阅: LINQ 至 SQL:GroupBy() 和 Max() 以获取具有最新日期的 object

And a simplified Fiddle here: https://do.netfiddle.net/yYMVNn这里有一个简化的小提琴: https://do.netfiddle.net/yYMVNn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM