开发者

sql query to linq-to-entities

开发者 https://www.devze.com 2023-01-13 06:15 出处:网络
How can this query be transform to linq SELECT materialId, SUM(totalAmount) as quantity FROM Inventory

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消