【发布时间】:2015-04-19 02:31:58
【问题描述】:
我想对数据库表中具有相同商品 ID 的所有行中的所有商品价格求和。我的意思是结果应该是一个数字,当我从下拉列表中选择特定项目 id 时,它会自动放在文本框中
如何在 linq 中做到这一点?
这是实现我想要的还是应该使用Group by。
告诉我正确答案是什么:
public double oIlstGetVouchersDetailjh(int nvoucherID)
{
vouchers ovouchers = new vouchers();
double sum = (from Entity in ovouchers.INV_InventoryItems
where Entity.ItemID == nvoucherID
select Entity.ItemPrice).Sum();
return sum;
}
【问题讨论】:
-
这个查询给你什么?你期待它给你什么?看起来应该没问题,但是您对数据的其他结构没有太多详细信息。
-
我没试过..我是编程的初学者,尤其是linq
-
ovouchers.INV_InventoryItems.Where(x => x.ItemID == nvoucherID).Select(x => x.ItemPrice).Aggregate((x,y) => x + y);