linq中的Sum数据库列 [英] Sum database column in linq
问题描述
大家好,
我尝试将总价格的总和加到发票网络应用程序中。我有数据基表与列价(代表每个项目的价格),我需要计算这一列的总和。我使用了sum属性但出现了错误:
指定演员表无效。
我的命令:
hi all,,
i am try to sum the value of total price to invoice web application. i have data base table with column price(represent the price to each item), i need to calculated the total of this column. i used sum property but an error appears :
Specified cast is not valid.
My Command:
var Total = gen.InvoiceDetails.Sum(x => x.TotalPrice);
其中gen.InvoiceDetails是数据库上下文和表,TotalPrice是我要求的列
问候
where gen.InvoiceDetails is the database context and table and TotalPrice is the column i want to sum
regards
推荐答案
试试这个
Try this
var Total = gen.InvoiceDetails.AsEnumerable().Sum(x=>x.Field<int>("TotalPrice"));
为TotalPrice提供正确的DataType。在这里我作为整数
Give the proper DataType for TotalPrice . here i take as Integer
你可以尝试下面的代码。
you can try below code.
var Total = gen.InvoiceDetails.ToList().Sum(x => x.TotalPrice);
如果有任何问题写下您的评论。
if any issue write your comment.
请澄清您的问题,
如果,我是对的,希望这会对您有所帮助。
var Total = gen.InvoiceDetails.Select(x => new InvoiceDetail {
TotalPrice = x.Field< int>(TotalPrice)
})。FirstOrDefault();
please clarify your question,
If,I'm right,Hope this helps you.
var Total= gen.InvoiceDetails.Select(x=>new InvoiceDetail{
TotalPrice=x.Field<int>("TotalPrice")
}).FirstOrDefault();
这篇关于linq中的Sum数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!