C# 将或添加到linq查询
是否可以在声明后向linq查询添加或 我的意思是如果我有:C# 将或添加到linq查询,c#,linq,C#,Linq,是否可以在声明后向linq查询添加或 我的意思是如果我有: Query.Where(f => some-condition ); 我想在后面加上“或”: if (some-condition) Query.WhereOr(f => some-condition ); 您应该使用LinqKit.PredicateBuilder类 例如: var predicate = PredicateBuilder.True<Product>(); if(inputPri
Query.Where(f => some-condition );
我想在后面加上“或”:
if (some-condition)
Query.WhereOr(f => some-condition );
您应该使用LinqKit.PredicateBuilder类 例如:
var predicate = PredicateBuilder.True<Product>();
if(inputPrice > 100)
predicate = predicate.And (p => p.Price > 100);
if(inputName == "S%")
predicate = predicate.And (p => p.Name.StartsWith("S"));
更多详细信息IEnumerable和IQueryable在某种程度上将允许您链接条件
Query.Where(f=> some-condition || some_other_condition);
// or
var q = Query.Where(f=> some-condition)
if(stuff_needs_to_happen)
q = q.Where(f => stuff_that_needs_to_happen)
“在某种程度上”的原因是,使用IQueryable,基础查询语言可能不支持谓词中的某些条件(自定义函数等)。看起来您可能正在寻找 或者您可以将一些.Where子句堆叠为linq是懒惰的 由于您的问题缺乏上下文,想象一下基于表单查询Person表(2个字段/1个复选框)
//在哪里堆叠
如果(!string.IsNullOrEmpty(FieldA.Text))
{//或
result=query.Where(item=>item.Something.Contains(FieldA.Text));
}
如果(!string.IsNullOrEmpty(FieldB.Text))
{//和
result=result.Where(item=>item.SomethingElse.Contains(FieldB.Text));
}
//谓词
如果(复选框1.选中| |复选框2.选中| |复选框3.选中))
{//在这里,您将谓词初始化为false,因为您将执行或
var predicate=PredicateBuilder.False();
if(CheckBox1.Checked)predicate=predicate.Or(item=>item.Foo==1);
if(CheckBox2.Checked)predicate=predicate.Or(item=>item.Foo==2);
如果(CheckBox3.Checked)谓词=谓词或(item=>item.Foo==3);
result=result.Where(谓词);
}
堆放在合适的位置是一个很好的解决方案,但可能会产生一点过热,但不会引起真正的注意
PS:Jon关于C代码嵌套的文章。您通常如何在C#中实现OR运算符?这是相同的:
其中(f=>某些条件| |某些其他条件)
。不要试图使事情过于复杂。Query.Where(f=>somecondition | | somecondition2)代码>?我想,在添加那个条件之前,先有一个if语句@jmcilhinneyi如果您保持查询
不变(不应用Where
的话),就可以使用类似Query.Where(cond1.Union(Query.Where(cond2))的东西
但我不知道结果是否与简单的|
类似,你的例子不应该是关于这个问题的或吗?我相信第二部分相当于查询。其中(f=>一些需要发生的条件和东西)
而不是查询(f=>some condition | | stuff|u需要发生)
不会堆叠。Where
子句表现为和而不是或?@Rafalon,不是堆叠导致和,而是**query**query**query**。Where
那么堆叠是什么意思呢?我试过整数。Where(x=>x>3)。Where(x=>x<5)
whereintegers=={1,2,3,4,5,6}
,它只返回4,就好像它是整数一样。where(x=>x>3&&x<5)
@Rafalon,我已经将我的想法编辑成了代码。但是你是对的,这种方式可以作为一个子查询,而不是一个关闭查询,这取决于很多事情。
Query.Where(f=> some-condition || some_other_condition);
// or
var q = Query.Where(f=> some-condition)
if(stuff_needs_to_happen)
q = q.Where(f => stuff_that_needs_to_happen)
// Where Stacking
if (!string.IsNullOrEmpty(FieldA.Text))
{// OR
result = query.Where(item => item.Something.Contains(FieldA.Text));
}
if (!string.IsNullOrEmpty(FieldB.Text))
{// AND
result = result.Where(item => item.SomethingElse.Contains(FieldB.Text));
}
// Predicate
if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked))
{//Here your initialise your predicate to false because you are going to do OR
var predicate = PredicateBuilder.False<SearchItem>();
if(CheckBox1.Checked) predicate = predicate.Or(item => item.Foo == 1);
if(CheckBox2.Checked) predicate = predicate.Or(item => item.Foo == 2);
if(CheckBox3.Checked) predicate = predicate.Or(item => item.Foo == 3);
result = result.Where(predicate);
}