Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

612
Visualizações
How can I convert my working SQL query to Linq (C#)?

I have a working SQL query that returns the results I want on SQL Server:

SELECT
  SUM(CriticalCount) As CriticalCount,
  SUM(HighCount) As HighCount,
  SUM(MediumCount) As MediumCount,
  SUM(LowCount) As LowCount,
  Timestamp
FROM [dbo].[TicketCounts]
GROUP BY Timestamp
ORDER BY Timestamp DESC

The result set is:

CriticalCount HighCount MediumCount LowCount Timestamp
32 15 18 3 2021-07-01 11:00:00
24 10 42 15 2021-06-30 10:00:00

In my TicketCountRepository of my .Net Core 5.0 project, I want this method to return the same results:

public async Task<IEnumerable<TicketCount>> GetTicketCounts()
{
  return await _dbContext.TicketCounts
      .GroupBy(o => o.Timestamp)
      .Select(tc => new TicketCount
      {
        CriticalCount = tc.Sum(o => o.CriticalCount),
        HighCount = tc.Sum(o => o.HighCount),
        MediumCount = tc.Sum(o => o.MediumCount),
        LowCount = tc.Sum(o => LowCount)
      }).OrderByDescending(o => o.Timestamp).toListAsync();
}

The code compiles fine, but when I hit this method in the TicketCount controller, I get a 500 error:

System.InvalidOperationException: The LINQ expression DbSet().GroupBy(keySelector: o => o.Timestamp, elementSelector o => o).Select(e => new TicketCount { CriticalCount = e.Sum(s => s.CriticalCount), HighCount = e.Sum(s => s.HighCount), MediumCount = e.Sum(s => s.MediumCount), LowCount = e.Sum(s => s.LowCount)}).OrderByDescending(e0 => e0.Timestamp) 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'.

What am I doing wrong? Should I use something other than linq to get the result set needed?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The type you're querying is wrong, it's the return type instead of the type containing the raw data. You want something like:

  return await _dbContext.Tickets
  .GroupBy(o => o.Timestamp)
  .Select(x => new TicketCount
  {
    Timestamp = x.Key,
    CriticalCount = x.Sum(o => o.CriticalCount),
    HighCount = x.Sum(o => o.HighCount),
    MediumCount = x.Sum(o => o.MediumCount),
    LowCount = x.Sum(o => LowCount)
  }).OrderByDescending(x => x.Timestamp).toListAsync();
over 4 years ago · Santiago Trujillo Relatório

0

Since you don't select timestamp move it before select

return await _dbContext.TicketCounts
      .GroupBy(o => o.Timestamp)
       .OrderByDescending(o => o.Timestamp)
      .Select(tc => new TicketCount
      {
        CriticalCount = tc.Sum(o => o.CriticalCount),
        HighCount = tc.Sum(o => o.HighCount),
        MediumCount = tc.Sum(o => o.MediumCount),
        LowCount = tc.Sum(o => LowCount)
      }).ToListAsync();

or you can try to add Timestamp property to TicketCount and add it to Select.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda