如何通过linq查询将数据库写入数据集? [英] How to write group by linq query to a dataset?
本文介绍了如何通过linq查询将数据库写入数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在DataSet中有员工详细信息。如何编写Linq查询按部门对员工进行分组并将其显示在网格中?
我尝试过:
I have employee details in DataSet. How to write Linq query to group employees by department and display them in grid?
What I have tried:
var names = from emp in dt.AsEnumerable()
group emp by new
{
ID = emp.Field<int>("UserId"),
} into g
select new
{
ID = g.Key.ID,
};
推荐答案
如果您想按部门分组数据,则必须使用正确的字段,例如DepartmentID
:
If you would like to group data by department, you have to use proper field, such asDepartmentID
:
var names = dt.AsEnumerable()
.GroupBy(emp=>emp.Field<int>("DepartmentId"))
.Select(grp=> new
{
DeptId = grp.Key,
//define other fields here
});
有关详细信息,请参阅:
C#中的101个LINQ样本 [ ^ ]
Linq 101 lambda样本 [ ^ ]
这篇关于如何通过linq查询将数据库写入数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文