在linq中,我将传递多个变量。在某些情况下,变量将为null。有没有办法有条件地删除每个"其中"如果var为null?例如:
var fooQuery = from s in _db.fooTable
// var A,B,C,D,E might be null
where s.a == varA
where s.b == varB
where s.c == varC
where s.d == varD
where s.f == varE
select s;
如果任何变量都为null我想跳过,忽略或传递那个"其中"。
让我更清楚。
varA和varB为null,因此查询需要更改:
var fooQuery = from s in _db.fooTable
//Ignore these 2 where because values are null but keep others
//ignore where s.a == varA
//ignore where s.b == varB
where s.c == varC
where s.d == varD
where s.f == varE
select s;
答案 0 :(得分:3)
只需在条件中包含null
支票:
where varA == null || s.a == varA