tags:

views:

52

answers:

2

Hello,

Im trying to write query in linq

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

Aby hints ?

+3  A: 
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 };
Anthony Pegram
Does this generate a single query? Or 1 query per group?
David B
it's a single query, when `User` is actually something like `dbContext.Users`
Sander Rijken
A: 

If subqueries are your thing, you could do it this way.

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;
David B