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

79
Visualizações
Join two tables and filter to retrieve Name

This code is performing well as expected.

var soldQtyForEachItem = await _context.InvoiceProduct
    .Where(x => x.ProductId != 0)
    .GroupBy(x => x.ProductId)
    .Select(grp => new CompanyProducts 
    { 
        CompanyProductId = grp.Key, 
        CompanyProductSoldQuantity = grp.Sum(item => item.QuantitySold)
    }).ToListAsync();
        

Question :

I now need to join another table called Products and filter by Id against InvoiceProduct table and retrieve ProductsItemName which is a row from Products table; then all need to go to a custom type "CompanyProducts" below.

Please how do I achieve it?


public class CompanyProducts
{
    public int CompanyProductId { get; set; }
    public int CompanyProductName { get; set; }
    public int CompanyProductSoldQuantity { get; set; }
}

InvoiceProduct table is a many to many, meaning that one InvoiceId may have multiple ProductIds.

Products table has property like ProductId, ProductsItemName.

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

0

Simplest solutions is to include ProductItemName into grouping:

var soldQtyForEachItem =  await _context.InvoiceProduct
    .Where(x => x.ProductId != 0)
    .GroupBy(x => new { x.ProductId, x.Product.ProductsItemName } )
    .Select(grp => new CompanyProducts 
    { 
        CompanyProductId = grp.Key.ProductId, 
        CompanyProductName = grp.Key.ProductsItemName,
        CompanyProductSoldQuantity = grp.Sum(item => item.QuantitySold)
    })
    .ToListAsync();
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