Search code examples
linqlambda

How to generate a lambda expression from this T-SQL code


I have SQL code like this:

select * 
from [groups]
where Id in (select GroupId from groupUsers where UserId = @UserId)

I want to generate a lambda code that returns the result exactly like the above code:

_context.Groups.where(g => g.Id == ...???)

Solution

  • I found the correct code. it is:

    _context.Groups.where(g => _context.GroupUsers.Any(u => u.GroupId == g.Id && u.UserId == @UserId))
    

    Regard to Rachel!