0

我正在学习linq。

我在 sql server 中有:

create table Something(id int, num int)

insert into Something(id, num) values(1, 10);
insert into Something(id, num) values(1, 10);
insert into Something(id, num) values(2, 7);
insert into Something(id, num) values(2, 7);
insert into Something(id, num) values(2, 7);
insert into Something(id, num) values(3, 5);
insert into Something(id, num) values(3, 5);
insert into Something(id, num) values(3, 5);
insert into Something(id, num) values(3, 5);
insert into Something(id, num) values(3, 5);
insert into Something(id, num) values(3, 5);
insert into Something(id, num) values(4, 5);

select id 
from something
group by id, num
having count(*) < num

结果是:4、2和1

我正在尝试使用 linq:

var query =
(from m in lst
group m by new { m.id, m.num } into g
where g.Count() < m.num).ToList();

我想将 g.Count() 与“num”字段进行比较,但我的代码有问题

感谢您的帮助!

4

1 回答 1

2

尝试g.key.num代替m.num

var query =
(from m in lst
group m by new { m.id, m.num } into g
where g.Count() < g.key.num).ToList();
于 2013-08-22T01:27:34.787 回答