LinQ& DataTable Group by C# [英] LinQ & DataTable Group By Question in C#
问题描述
var query = from in in incoming.AsEnumerable ()
group s by s.Field< string>(Stock card Code)
into grp
orderby grp.Key
select new {StockCardCode = grp.Key,Quantity = grp.Sum(r => r.Field< decimal>(Quantity))};
传入:DataTable
由于您是通过StockCardQuote进行分组,因此Explain值将是具有相同StockCardQuote的行的所有解释值 - 它将成为 IEnumerable< T>
,所以如果说是一个字符串
,你会得到 IEnumerable< string>
。
要将其包含在您的选择
语句中,只需执行
select new {
StockCardCode = grp.Key,
Quantity = grp.Sum(r => r。字段< decimal>(Quantity)),
说明= grp.Select(r => r.Field< string>(Explain))};
I have a DataTable and stores 4 columns (StockCardCode,Explain,Quantity,BranchCode), and Im grouping them with a LinQ. Code is my key for using Group By. When I write my query, I just can use 2 columns in select statement, how can I show all columns in this query? This query returns me 2 column. I cant use x.Field("Explain") in my query for example.
var query = from s in incoming.AsEnumerable()
group s by s.Field<string>("Stock Card Code")
into grp
orderby grp.Key
select new { StockCardCode = grp.Key, Quantity = grp.Sum(r => r.Field<decimal>("Quantity")) };
incoming: DataTable
Since you are grouping by StockCardQuote, the "Explain" value is going to be all of the Explain values from rows with the same StockCardQuote -- it is going to be an IEnumerable<T>
, so if Explain is a string
, you'll get IEnumerable<string>
.
To include this in your select
statement, just do
select new {
StockCardCode = grp.Key,
Quantity = grp.Sum(r => r.Field<decimal>("Quantity")),
Explanations = grp.Select(r => r.Field<string>("Explain")) };
这篇关于LinQ& DataTable Group by C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!