linq group by with count

时间:2010-06-13 18:51:12

标签: linq

我试图在linq中编写查询

Select UserId, UserNumber 
FROM User
where UserNumber in
(Select UserNumber
        FROM User
        group by UserNumber
        having Count(UserId) = 1)

Aby暗示?

2 个答案:

答案 0 :(得分:8)

var query = from u in User
            group u by u.UserNumber into usergroup
            where usergroup.Count() == 1
            from item in usergroup
            select new { item.UserId,item.UserNumber };

答案 1 :(得分:0)

如果子查询是你的事,你可以这样做。

var subquery =
  from u in User
  group u by u.UserNumber into g
  where g.Count() = 1
  select g.Key;

var query =
  from u in User
  where subquery.Any(num => num == u.UserNumber)
  select u;