Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ到entites max按日期选择max item EF Core 3_C#_Linq_Entity Framework Core - Fatal编程技术网

C# LINQ到entites max按日期选择max item EF Core 3

C# LINQ到entites max按日期选择max item EF Core 3,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,如何在select new语句Post中选择具有最大日期的 var result = from category in dataRepository.Categories from forum in category.Forums from topic in forum.Topics from post in topic.Posts group new { ca

如何在
select new
语句
Post
中选择具有最大日期的

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?*/
                 };
我试过了

        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()
                     };
获取错误

.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.
第二次尝试

        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
                     };
错误

另见:

这里有一把简化的小提琴:

.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