cegano
cegano

Reputation: 11

Convert SQL to Linq using group by

I am beginner in linq, I want to convert this SQL query:

select UserId 
from Table 
where sID in (123, 124) 
group by UserId 
having count(distinct sID) = 2

I tried like this

var abc = obj_map.dbset
                 .where(x => sID.Contains(x.sID.ToString()))
                 .Select(m => UserId)
                 .Distinct()
                 .ToList();

How I can solve this?

Upvotes: 1

Views: 32

Answers (1)

Enigmativity
Enigmativity

Reputation: 117064

You probably want something more like this:

int[] sIDs = new[] { 123, 124 };

IEnumerable<IGrouping<string, X>> abc =
    obj_map
        .dbset
        .Where(x => sIDs.Contains(x.sID))
        .GroupBy(x => x.UserId)
        .Where(gs => gs.Count() == 2);

Upvotes: 1

Related Questions