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