views:

31

answers:

1

How can this query be transform to linq

SELECT materialId, SUM(totalAmount) as quantity FROM Inventory

It's the sum part that I don't know how...

  query = from inv in context.Inventory
                        select new MaterialQuantity() 
                        {
                            MaterialId = inv.materialId,
                            Quantity = ??
                        };

EDIT Trying to sum the value of totalAmount.

It's a view that is

materialId totalSum and other fields
1          5
1          10
1          20

So I want my linq to return me MaterialId = 1, Quantity = 35

+1  A: 

I'm going to give a complete guess here... assuming your inventory has multiple rows with the same materialId and you want to sum in those groups, you could use:

var query = from inv in content.Inventory
            group inv.totalAmount by inv.materialId into g
            select new { MaterialId = g.Key, Quantity = g.Sum() };

If you're not trying to group though, you'll need to clarify your question. Sample data and expected output would help.

Jon Skeet