LINQ to Datatable分组并返回所有列 [英] LINQ to Datatable Group by and return all columns
本文介绍了LINQ to Datatable分组并返回所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataTable
ID年龄姓名性别类Father_Name Mother_Name标记
1月20日反导1 AB 50
1月20日反导1 AB 50
1月20日反导1 AB 100
2月15日FGF 2 HJ 40
2月15日FGF 2 HJ 50
&安培;我想通过
1月20日反导1 AB 200
2月15日FGF 2 HJ $ 90申请的LINQ集团经过以下结果b $ b
我能够得到的总和,但我不能够通过<得到的LINQ组其他列/ p>
任何人都可以在此
帮助
VAR drResults =从R1在dtResults .AsEnumerable()
组R1被新的
{
MarksSum = r1.Field< INT>(标志),
}
为G
选择新的
{
总= g.Sum(R1 => r1.Field< INT>(标志))
} ;
解决方案
VAR _result =从()R1在dtResults.AsEnumerable通过新的
{
ID = r1.Field<
组R1; INT>(ID),
年龄= r1.Field< INT>(时代),
姓氏= r1.Field< INT>(姓氏),
名字= r1.Field< INT>(名字),
性别= r1.Field< INT>(性),
级= r1.Field< INT>(类),
Father_Name = r1.Field< INT>(Father_Name),
Mother_Name = r1.Field< INT>(Mother_Name)
}到G
选择新的
{
ID = g.Key.ID,
时代= g.Key.Age,
姓氏= g.Key.LastName,
名字= g.Key.FirstName,
性别= g.Key.Sex,
级= G .Key.Class,
Father_Name = g.Key.Father_Name,
Mother_Name = g.Key.Mother_Name,
TotalMark = g.Sum(X => x.Field< INT>(标志))
}
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
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
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"))
};
解决方案
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"))
}
这篇关于LINQ to Datatable分组并返回所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文