LINQ WHERE语句/忽略条件 [英] LINQ WHERE statement/ignore conditions
本文介绍了LINQ WHERE语句/忽略条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果参数为null或为空,我需要忽略WHERE语句中的部分或全部条件 F.E:
I need to ignore some or all conditions in WHERE statement if parameter is null or empty F.E:
我有简单的LINQ查询
I have simple LINQ query
var query = from x in context.a
where x.p == param1 && x.i == param2
select x;
如果param1为null或emty,如何忽略x.p == param1
?
How can I ignore x.p == param1
if param1 is null or emty?
尝试过
var query = from myLog in myContext.ApsValidationLogs
where (myLog.systemtype == comboBoxSystemType.SelectedItem.ToString() || string.IsNullOrEmpty(comboBoxSystemType.SelectedItem.ToString()))
&& (myLog.bankid == comboBoxBankId.SelectedItem.ToString() || string.IsNullOrEmpty(comboBoxBankId.SelectedItem.ToString())))
select myLog;
但是得到了
Object reference not set to an instance of an object.
如果第二个组合框的项目为null.怎么了?
In case if second combobox's item is null. What's wrong?
推荐答案
您可以将其添加为条件:
You can add it as a condition:
var query= from x in context.a
where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
select x;
如果param1
为null或为空,则条件将始终为true,从而有效地完全忽略" where条件.
If param1
is null or empty, the condition will always be true, which effectively "ignores" the where conditions entirely.
这篇关于LINQ WHERE语句/忽略条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文