总数据表列使用linq [英] Sum data table columns using linq
问题描述
所有我在datatable中的数据如下
SKU数量UnitPrice LinePrice
A 10 2 20
A 10 2 20
B 10 2 40
我想要总结重新记录SKU并得到如下结果
SKU数量UnitPrice LinePrice
A 20 2 40
B 10 2 40
我尝试过但没有运气
var query = from lDTSalesOrder.AsEnumerable()
group row by row.Field< string>(SKU)into grp
orderby grp.Key
选择新
{
Id = grp.Key,
Sum = grp.Sum(r => r.Field< double>(UnitPrice) )
};
在您的示例输出中,实际上并没有总结单价,但它听起来像你想要的是这样的:
var results =
from l in lDTSalesOrder
group row by row.Field< string>(SKU)into grp
orderby grp.Key
选择新
{
SKU = grp.Key,
数量= grp.Sum(r => r.Field< double>(Quantity)),
UnitPrice = grp.Sum(r => r.Field< double>(UnitPrice )),
LinePrice = grp.Sum(r => r.Field< double>(LinePrice))
};
这将产生:
SKU数量UnitPrice LinePrice
A 20 4 40
B 10 2 40
如果您真的想要生成 2
的单价第一个结果记录 Sum
不是你想要的;你必须使用一些其他聚合函数。 最小
,最大
,平均
甚至 Select(...)。First()
会产生这个结果,但是你必须更准确地定义你想要的输出。您也可以这样做:
var results =
from lDTSalesOrder
group row by new { SKU = row.Field< string>(SKU),UnitPrice = row.Field< string>(UnitPrice)} into grp
orderby grp.Key.SKU
select new
{
SKU = grp.Key.SKU,
数量= grp.Sum(r => r.Field< double>(Quantity)),
UnitPrice = grp.Key。 UnitPrice,
LinePrice = grp.Sum(r => r.Field< double>(LinePrice))
};
任何这些选项将产生:
SKU数量UnitPrice LinePrice
pre>
A 20 2 40
B 10 2 40
Hi all I am having my data in datatable as follows
SKU Quantity UnitPrice LinePrice A 10 2 20 A 10 2 20 B 10 2 40
I would like to sum up the SKU with duplicate records and get the result as follows
SKU Quantity UnitPrice LinePrice A 20 2 40 B 10 2 40
I tried this but no luck
var query = from row in lDTSalesOrder.AsEnumerable() group row by row.Field<string>("SKU") into grp orderby grp.Key select new { Id = grp.Key, Sum = grp.Sum(r => r.Field<double>("UnitPrice")) };
解决方案In your sample output you haven't actually summed the Unit Price, but it sounds like what you want is something like this:
var results = from row in lDTSalesOrder group row by row.Field<string>("SKU") into grp orderby grp.Key select new { SKU = grp.Key, Quantity = grp.Sum(r => r.Field<double>("Quantity")), UnitPrice = grp.Sum(r => r.Field<double>("UnitPrice")), LinePrice = grp.Sum(r => r.Field<double>("LinePrice")) };
This will produce:
SKU Quantity UnitPrice LinePrice A 20 4 40 B 10 2 40
If you really want to produce a Unit Price of
2
for the first result record,Sum
is not what you want; you'll have to to use some other aggregate function.Min
,Max
,Average
, or evenSelect(...).First()
would produce that result, but you'd have to more precisely define what you want in your output. You could also do this:var results = from row in lDTSalesOrder group row by new { SKU = row.Field<string>("SKU"), UnitPrice = row.Field<string>("UnitPrice") } into grp orderby grp.Key.SKU select new { SKU = grp.Key.SKU, Quantity = grp.Sum(r => r.Field<double>("Quantity")), UnitPrice = grp.Key.UnitPrice, LinePrice = grp.Sum(r => r.Field<double>("LinePrice")) };
Any of these options will produce:
SKU Quantity UnitPrice LinePrice A 20 2 40 B 10 2 40
这篇关于总数据表列使用linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!