locked
INNER JOIN and GROUP BY RRS feed

  • Question

  • User521171331 posted

    Hi, how do I convert this query into linq query?

    select c.categoryname, sum(unitprice) from Products p
    inner join Categories c on p.CategoryID = c.CategoryID
    group by c.Categoryname

    This is what i got:

    var groupbyResultJoin = (from product in dc.Products
    join category in dc.Categories
    on product.CategoryID equals category.CategoryID
    group product by product.CategoryID into productCategory
    select new { Category = productCategory.Key, Total = productCategory.Sum(s => s.UnitPrice) });

    But, i want to select category name instead of category id. if i change it at group, then i can't get the unit price from product table.

    Friday, April 20, 2018 8:14 AM

Answers

  • User521171331 posted

    It's okay. Solved.

    var groupbyResultJoin = (from product in dc.Products
    join category in dc.Categories
    on product.CategoryID equals category.CategoryID
    group product by new { product.CategoryID, product.Category.CategoryName } into productCategory
    select new { Category = productCategory.Key.CategoryName, Total = productCategory.Sum(s => s.UnitPrice) });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 20, 2018 8:18 AM