[英]Nested WHERE IN clause SQL to LINQ
I have a nested WHERE IN clause in my SQL, how would this translate to LINQ, bonus points for using lambda expressions. 我在SQL中有一个嵌套的WHERE IN子句,这将如何转换为LINQ,即使用lambda表达式的加分点。 New to all of this. 所有这些都是新的。
SELECT EndowmentID
FROM Criteria c
WHERE c.ID IN(
SELECT CriterionID
FROM Filters
WHERE ChoiceID IN(
SELECT ChoiceID
FROM Responses
WHERE ApplicationID = 1
)
)
This query can definitely be improved using joins... 绝对可以使用联接来改善此查询...
SELECT EndowmentID
FROM
Criteria C
JOIN Filters F ON C.ID = F.CriterionID
JOIN Responses R ON F.ChoiceID = R.ChoiceID
WHERE R.ApplicationID = 1
Depending on the keys of your tables you might have to SELECT DISTINCT 根据表的键,您可能需要选择DISTINCT
From there you can write a simple LINQ query: 从那里可以编写一个简单的LINQ查询:
from c in Criteria
join f in Filters on c.ID equals f.CriterionID
join r in Responses on f.ChoiceID equals r.ChoiceID
where r.ApplicationID = 1
select c.EndowmentID
Again, you might have to Distinct() this. 同样,您可能必须对此进行Distinct()。
var result =
Criteria.Where(c =>
Filters.Where(f =>
Responses.Where(r => r.ApplicationId == 1).Select(r => r.ChoiceId)
.Contains(f.ChoiceId)
).Select(f => f.CriterionId)
.Contains(c.Id)
).Select(c => EndowmentId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.