Linq 是不是可以实现这样的功能?

健者天行 2011-06-18 10:35:38
Linq 是不是可以实现这样的功能?


DataTable dt = new DataTable();
da.Fill(dt);

假设数据已装载进入内存,dt表中有一个字段 EmployeeCode ,
想在内存的 dt 中实现如下的SQL 语句的功能:


select
EmployeeCode,
count(EmployeeCode) [nn]
from employeeMsg
group by EmployeeCode
having count(EmployeeCode) > 1


是不是 Linq 可以做到?
貌似这样,但语法有错呀。
请高手指点。

var q =
from p in dt.AsDataView()
group p by p.EmployeeCode into g
select new
{
g.EmployeeCode,
Num = g.Count()
};
...全文
107 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
健者天行 2011-06-18
  • 打赏
  • 举报
回复
#11楼,可惜已结贴不能给你分了。
threenewbee 2011-06-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sp1234 的回复:]
可以写得更清晰一点

C# code

var query = from DataRow r in dt.Rows
let employee = (string)r["EmployeeCode"]
group r by employee into g
where g.Count() > 1
……
[/Quote]
是的,呵呵。
  • 打赏
  • 举报
回复
可以写得更清晰一点
var query = from DataRow r in dt.Rows
let employee = (string)r["EmployeeCode"]
group r by employee into g
where g.Count() > 1
select new { EmployeeCode = g.Key, Count = g.Count() };
健者天行 2011-06-18
  • 打赏
  • 举报
回复
可以了,
谢谢 caozhy

结帐
暖枫无敌 2011-06-18
  • 打赏
  • 举报
回复
还有个分组的:

var q = from p in dt.AsEnumerable()
group p by p.Field<string>("EmployeeCode")
where p.Field<string>("EmployeeCode").Count()>1
select new
{
EmployeeCode = p.Field<string>("EmployeeCode"),
CountNumber = p.Field<string>("EmployeeCode").Count()
};
threenewbee 2011-06-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 leo2003 的回复:]
#5楼的可以了。

谢谢

再请教一下:

if ( q.个数 > 0 ) //我想在这里判断一下 q 集合的个数,这里怎么写?
{
foreach (var item in q)
{
MessageBox.Show(string.Format( "工号:{0},人数:{1}" ,item.Code,item.Num) );
}
//其他代码.

}
[/Quote]
什么意思?

if (q.Count() > 0)
...
健者天行 2011-06-18
  • 打赏
  • 举报
回复
#5楼的可以了。

谢谢

再请教一下:

if ( q.个数 > 0 ) //我想在这里判断一下 q 集合的个数,这里怎么写?
{
foreach (var item in q)
{
MessageBox.Show(string.Format( "工号:{0},人数:{1}" ,item.Code,item.Num) );
}
//其他代码.

}
暖枫无敌 2011-06-18
  • 打赏
  • 举报
回复

var q =from p in dt.AsEnumerable()
where p.Field<int>("EmployeeCode").Count()>1
select new
{
EmployeeCode = p.Field<string>("EmployeeCode"),
CountNumber = p.Field<int>("EmployeeCode").Count()
};
threenewbee 2011-06-18
  • 打赏
  • 举报
回复
var q =
(from p in dt.Rows.Cast<DataRow>()
group p by p["EmployeeCode"].ToString() into g
select new
{
Code = g.Key,
Num = g.Count()
}).Where(x => x.Num > 1);
threenewbee 2011-06-18
  • 打赏
  • 举报
回复
var q =
from p in dt.Rows.Cast<DataRow>()
group p by p["EmployeeCode"].ToString() into g
select new
{
Code = g.Key,
Num = g.Count()
};
健者天行 2011-06-18
  • 打赏
  • 举报
回复
还要怎么加上这句:
having count(EmployeeCode) > 1

也就是只想查出 有2个及以上相同的工号。
健者天行 2011-06-18
  • 打赏
  • 举报
回复
回楼上:

dt.Rows 提示错误:

Error 1 Could not find an implementation of the query pattern for source type 'System.Data.DataRowCollection'. 'GroupBy' not found. Consider explicitly specifying the type of the range variable 'p'. E:\Framework\HR.GrandTai\frmRequestHours.cs 158 31 HR.GrandTai


threenewbee 2011-06-18
  • 打赏
  • 举报
回复
var q =
from p in dt.Rows
group p by p["EmployeeCode"].ToString() into g
select new
{
Code = g.Key,
Num = g.Count()
};

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧