Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!