Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda