Eliza
Eliza

Reputation: 135

Convert Join and Group By from SQL to LINQ

I'm new with LINQ and I need help Converting this SQL Code: To LINQ

    SELECT UserId_FK,COUNT(OrderId),SUM(Quantity)
    FROM Orders o
    JOIN OrderDetails od
    ON(o.OrderId = od.OrderId_FK)
    GROUP BY UserId_FK;

Upvotes: 1

Views: 139

Answers (1)

lsuarez
lsuarez

Reputation: 4992

Without seeing your schema file, I'm going to take a guess here:

var userDetail =
    context.Orders
        .GroupBy(i => i.UserId_Fk)
        .Select(i => new {
                         UserId_Fk = i.Key,
                         OrderCount = i.Count(),
                         ProductQuantity = i.Sum(j => context.OrderDetails.Where(k=> k.OrderId_Fk == j.OrderId).Sum(k=> k.Quantity))
                      });

Upvotes: 3

Related Questions