如果参数为 null 或为空,我需要忽略 WHERE 语句中的部分或全部条件
FE:
我有简单的 LINQ 查询
var query = from x in context.a
where x.p == param1 && x.i == param2
select x;
我怎能忽视x.p == param1
如果参数为 null 或为空?
EDIT
试过这个
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;
But got
Object reference not set to an instance of an object.
如果第二个组合框的项目为空。怎么了?
您可以将其添加为条件:
var query= from x in context.a
where String.IsNullOrEmpty(param1) || (x.p == param1 && x.i == param2)
select x;
If param1
为 null 或为空时,条件将始终为 true,这实际上完全“忽略”了 where 条件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)