C# LINQ to Datatable Group by 并返回所有列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15448222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ to Datatable Group by and return all columns
提问by vicky
I have a Datatable
我有一个数据表
ID Age Last name First Name Sex class Father_Name Mother_Name Marks
1 20 A B M 1 A B 50
1 20 A B M 1 A B 50
1 20 A B M 1 A B 100
2 15 F G F 2 H J 40
2 15 F G F 2 H J 50
& I want following results after applying Linq Group by
&我希望在应用 Linq Group 后获得以下结果
1 20 A B M 1 A B 200
2 15 F G F 2 H J 90
I am able to get the sum but i am not able to get the other columns in LINQ group by
我能够得到总和,但我无法得到 LINQ 组中的其他列
Can anyone help on this
谁能帮忙解决这个问题
var drResults = from r1 in dtResults.AsEnumerable()
group r1 by new
{
MarksSum= r1.Field<int>("Marks"),
}
into g
select new
{
Total = g.Sum(r1=>r1.Field<int>("Marks"))
};
采纳答案by John Woo
var _result = from r1 in dtResults.AsEnumerable()
group r1 by new
{
ID = r1.Field<int>("ID"),
Age = r1.Field<int>("Age"),
LastName = r1.Field<int>("LastName"),
FirstName = r1.Field<int>("FirstName"),
Sex = r1.Field<int>("Sex"),
Class = r1.Field<int>("class"),
Father_Name = r1.Field<int>("Father_Name"),
Mother_Name = r1.Field<int>("Mother_Name")
} into g
select new
{
ID = g.Key.ID,
Age = g.Key.Age,
LastName = g.Key.LastName,
FirstName = g.Key.FirstName,
Sex = g.Key.Sex,
Class = g.Key.Class,
Father_Name = g.Key.Father_Name,
Mother_Name = g.Key.Mother_Name,
TotalMark = g.Sum(x => x.Field<int>("Marks"))
};
回答by Tim Schmelter
You want to group by the Id
?! Then you can use Sum
the Marks
on the group:
你想分组Id
吗?!然后你可以使用Sum
在Marks
该组:
var result = from r in dtResults.AsEnumerable()
group r by r.Field<int>("ID") into IdGroup
let row = IdGroup.First()
select new
{
ID = IdGroup.Key,
Age = row.Field<int>("Age"),
LastName = row.Field<string>("Last_Name"),
FirstName = row.Field<string>("First_Name"),
MotherName = row.Field<string>("Mother_Name"),
FatherName = row.Field<string>("Father_Name"),
Marks = IdGroup.Sum(r => r.Field<int>("Marks"))
};
Update
更新
Hey Tim, if all rows have different values then what should i do? e.g LastName is different and ID is same. I have some data which is corrupt or you can say redundant in that case i am asking.
嘿蒂姆,如果所有行都有不同的值,那么我该怎么办?例如,姓氏不同,ID 相同。我有一些已损坏的数据,或者在这种情况下您可以说是多余的。
It's out of the scope of this question to show how you can "repair" your DataTable
. But if you instead want to list all rows even when they have the same ID, but witht he correct summed Mark
per Id-Group, you can use this query:
展示如何“修复”您的DataTable
. 但是,如果您想列出所有行,即使它们具有相同的 ID,但Mark
每个 Id-Group 的总和正确,您可以使用以下查询:
var result = from r in dtResults.AsEnumerable()
group r by r.Field<int>("ID") into IdGroup
let IdTotalMarks = IdGroup.Sum(r => r.Field<int>("Marks"))
from row in IdGroup
select new
{
ID = IdGroup.Key,
Age = row.Field<int>("Age"),
LastName = row.Field<string>("Last Name"),
FirstName = row.Field<string>("First Name"),
MotherName = row.Field<string>("Mother Name"),
FatherName = row.Field<string>("Father_Name"),
Marks = IdTotalMarks
};
回答by Oliver
JW's answer with Lambda syntax (I have also renamed the field "class" as this won't compile):
JW 对 Lambda 语法的回答(我还重命名了字段“class”,因为这不会编译):
var _result = dtResults
.AsEnumerable()
.GroupBy(r1 => new
{
ID = r1.Field<int>("ID"),
Age = r1.Field<int>("Age"),
LastName = r1.Field<string>("LastName"),
FirstName = r1.Field<string>("FirstName"),
Sex = r1.Field<int>("Sex"),
Class = r1.Field<int>("class"),
Father_Name = r1.Field<string>("Father_Name"),
Mother_Name = r1.Field<string>("Mother_Name")
}).Select(g => new
{
ID = g.Key.ID,
Age = g.Key.Age,
LastName = g.Key.LastName,
FirstName = g.Key.FirstName,
Sex = g.Key.Sex,
Class = g.Key.Class,
Father_Name = g.Key.Father_Name,
Mother_Name = g.Key.Mother_Name,
TotalMark = g.Sum(x => x.Field<int>("Marks"))
});