LINQ для максимального выбора максимального элемента по дате EF Core 3 - PullRequest
1 голос
/ 05 мая 2020

Как выбрать в 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.

1 Ответ

1 голос
/ 05 мая 2020
  Post = resultSet.OrderByDescending(i=>i.DateTime).Select(i=>i).FirstOrDefault().post

См. Также: LINQ to SQL: GroupBy () и Max (), чтобы получить объект с последней датой

И здесь упрощенный Fiddle: https://dotnetfiddle.net/yYMVNn

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...