I'm trying to create a subquery on my code and was able to create a subquery but I cannot understand some parts.
Below is the subquery that I followed and my comments on what I understand.
List<int> IdsToFind = new List<int>() {2, 3, 4};
db.Users
.Where(u => SqlMethods.Like(u.LastName, "%fra%"))
.Where(u =>
db.CompanyRolesToUsers // the table inside the subquery
.Where(crtu => IdsToFind.Contains(crtu.CompanyRoleId)) //the filter inside the subquery
.Select(crtu => crtu.UserId) //I cannot understand why there is a select of column here
.Contains(u.Id) //I cannot understand the additional filter here
)
Below is the sample query of the above LINQ
SELECT * FROM Users WHERE Users.lastname LIKE '%fra%'AND Users.Id IN (
SELECT UserId
FROM CompanyRolesToUsers
WHERE CompanyRoleId in (2,3,4))
The code is from How to do subquery in linq
PS: I used the codes of others so that I could give a simple sample of my question. My code is complicated. Thank you.