Sortowanie malejące po dacie stworzenia

0

Napisałem coś takiego:

var result =
  await _dbContext.HistoricalOrder
          .AsNoTracking()
          .Where(x => x.RegistrationFormId == query.RegistrationFormId)
          .GroupBy(x => x.RegistrationFormFlowId)
          .Skip(((int)query.PaginationParameters.PageNumber - 1) * (int)query.PaginationParameters.PageSize)
          .Take((int)query.PaginationParameters.PageSize)
          .Select(x => new ChangeHistoryItemDto
          {
              CreatedAt = x.OrderByDescending(x => x.CreatedAt).FirstOrDefault().CreatedAt,
              CreatedBy = $"{x.OrderByDescending(x => x.CreatedAt).FirstOrDefault().Creator.FirstName} {s.ToList().First().Creator.LastName}",
              RegistrationFormFlowId = x.OrderByDescending(x => x.CreatedAt).FirstOrDefault().RegistrationFormFlowId
          })
          .OrderBy(x => x.CreatedAt)
          .ToListAsync();

I dostaję coś takiego:

The LINQ expression 'GroupByShaperExpression:
KeySelector: h.RegistrationFormFlowId, 
ElementSelector:EntityShaperExpression: 
   EntityType: HistoricalOrder
   ValueBufferExpression: 
       ProjectionBindingExpression: EmptyProjectionMember
   IsNullable: False

   .OrderByDescending(x => x.CreatedAt)' 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 
  'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information

Jak rozwiązać ten problem?

0
kzkzg napisał(a):

No masz napisane co i jak. Najprościej będzie jak zrobisz ToListAsync czy tam AsEnumerable przed OrderBy i potem jeszcze raz ToList.

var result =
  await _dbContext.HistoricalOrder
          .AsNoTracking()
          .Where(x => x.RegistrationFormId == query.RegistrationFormId)
          .GroupBy(x => x.RegistrationFormFlowId)
          .Skip(((int)query.PaginationParameters.PageNumber - 1) * (int)query.PaginationParameters.PageSize)
          .Take((int)query.PaginationParameters.PageSize)
          .Select(x => new ChangeHistoryItemDto
          {
              CreatedAt = x.AsEnumerable().OrderByDescending(x => x.CreatedAt).ToList().FirstOrDefault().CreatedAt,
              CreatedBy = $"{x.AsEnumerable().OrderByDescending(x => x.CreatedAt).ToList().FirstOrDefault().Creator.FirstName} {s.AsEnumerable().OrderByDescending(x => x.CreatedAt).ToList().FirstOrDefault().Creator.LastName}",
              RegistrationFormFlowId = x.AsEnumerable().OrderByDescending(x => x.CreatedAt).ToList().FirstOrDefault().RegistrationFormFlowId
          })
          .OrderBy(x => x.CreatedAt)
          .ToListAsync();

Expression of type 'System.Collections.Generic.List1[Domain.Entities.HistoricalOrder]' cannot be used for parameter of type 'System.Linq.IQueryable1[Domain.Entities.HistoricalOrder]'
of method 'Domain.Entities.HistoricalOrder FirstOrDefaultHistoricalOrder' (Parameter 'arg0')

No nie bardzo...

0

To niesety raczej nie zadziała. Musiałbś materializować listę po Take(). A całege selecta robić na cliencie.
https://social.msdn.microsoft.com/Forums/en-US/2b7e21bd-46be-412b-8c1e-66e5352c8a81/select-one-recent-row-for-that-record-with-group-by-two-columns?forum=aspadoentitylinq

0

A w taki sposób nie zadziała ?

var result =
  await _dbContext.HistoricalOrder
          .AsNoTracking()
          .Where(x => x.RegistrationFormId == query.RegistrationFormId)
          .GroupBy(x => x.RegistrationFormFlowId) 
          .Select(x => new 
                  {
                      RegistrationFormFlowId = x.Key,
                      Item = x.OrderByDescending(x => x.CreatedAt).FirstOrDefault() // lub x.MaxBy(x => x.CreatedAt) dla .NET6+
                  })
          .Select(x => new ChangeHistoryItemDto
          {
              CreatedAt = x.Item.CreatedAt,
              CreatedBy = $"{x.Item.Creator.FirstName} {x.Item.Creator.LastName}",
              RegistrationFormFlowId = x.RegistrationFormFlowId,
          })
           .Skip(((int)query.PaginationParameters.PageNumber - 1) * (int)query.PaginationParameters.PageSize)
          .Take((int)query.PaginationParameters.PageSize)
          .OrderBy(x => x.CreatedAt)
          .ToListAsync();

1 użytkowników online, w tym zalogowanych: 0, gości: 1