LINQ WHERE与OR [英] LINQ WHERE with OR

查看:50
本文介绍了LINQ WHERE与OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用LINQ来创建我的where子句,如下所示:

I use LINQ to create my where clause like so:

var query = from x in context.Xs
            select x;

if (y == ...)
{
    query = query.Where(x => x.Y == 1);
}

我有一堆这样的"if .... where"语句.我的问题是所有这些wheres都使用AND联接where子句,但是我需要我所有的where子句才能使用OR.有没有简单的方法可以将此代码移植到OR代码所在的位置?甚至用OR做到这一点的最简单方法是什么?

I have bunch of these "if .... where" statements. The issue I have is that all of those wheres join where clauses using AND but I need all my where clauses to use OR. Is there an easy way to port this code into where OR code? Or even what is the easiest way to do this with OR?

谢谢.

推荐答案

PredicateBuilder 是完美解决您的问题的方法.它使您可以继续将各个"AND"和"OR"语句添加在一起.

PredicateBuilder is the perfect solution for your problem. It allows you to keep adding individual "AND" as well as "OR" statements together.

这篇关于LINQ WHERE与OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆