【发布时间】:2016-03-21 10:51:23
【问题描述】:
我正在尝试在我的代码上创建一个子查询,并且能够创建一个子查询,但我无法理解某些部分。
以下是我遵循的子查询以及我所理解的内容。
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
)
以下是上述 LINQ 的示例查询
SELECT * FROM Users WHERE Users.lastname LIKE '%fra%'AND Users.Id IN (
SELECT UserId
FROM CompanyRolesToUsers
WHERE CompanyRoleId in (2,3,4))
代码来自How to do subquery in linq
PS:我使用了其他人的代码,以便我可以提供一个简单的问题示例。我的代码很复杂。谢谢。
【问题讨论】:
标签: c# asp.net-mvc linq