小编Ars*_*Ars的帖子

LINQ WHERE语句/忽略条件

如果参数为null或空FE,我需要忽略WHERE语句中的部分或全部条件:

我有简单的LINQ查询

var query = from x in context.a
            where x.p == param1 && x.i == param2
            select x;
Run Code Online (Sandbox Code Playgroud)

x.p == param1如果param1为null或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;
Run Code Online (Sandbox Code Playgroud)

但得到了

Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)

如果第二个组合框的项目为空.怎么了?

c# linq ignore where conditional-statements

7
推荐指数
2
解决办法
6912
查看次数

标签 统计

c# ×1

conditional-statements ×1

ignore ×1

linq ×1

where ×1