如何使用 LINQ 在多个 OR 内创建 SQL 嵌套 AND
我正在尝试从下面的 SQL 示例创建等效的 LINQ 查询:
SELECT *
FROM FOO
WHERE
((a == <val1>) AND (b == <val2>) AND (c == <val3>))
OR
((a == <val4>) AND (b == <val5>) AND (c == <val6>))
总会有 a、b 和 c 被 AND 组合在一起,并被 OR 包围。此模式可以出现 n 次。
我发现唯一可行的解决方案是使用 LINQ Union,但生成的 SQL 不是我想要的。
I'm trying to create the equivelant LINQ query from the below SQL example:
SELECT *
FROM FOO
WHERE
((a == <val1>) AND (b == <val2>) AND (c == <val3>))
OR
((a == <val4>) AND (b == <val5>) AND (c == <val6>))
There will always be an a, b, and c that will be AND'd together surrounded by an OR. This pattern can occur n amount of times.
The only solution I have found that work is using LINQ Union but the SQL generated isn't what I would like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试实现
PredicateBuilder
类。那么也许你可以使用这样的东西:
然后假设你的标准是可枚举的。根据需要进行修改以满足您的需求。
Try implementing the
PredicateBuilder
class.Then perhaps you can use something like this:
This then assumes your criteria is enumerable. Modify as you need to accommodate your needs.
您可以制作一个很长的条件语句:
或者,在我看来,哪个更好:
这些也可以是有条件的:
You can make a long conditional statement:
Or, which is preferable IMO:
These can be conditional also: