我的代码:
IEnumerable<DataRow> whrRowEnum;
whrRowEnum = from r in dtInput.AsEnumerable()
where r.Field<string>("EMP_DEP") == "DEP1"
orderby EMP_DEP
select r;
由于硬编码where条件,上面的代码工作正常,但在运行时我需要在我的linq查询中添加多个where条件,如r.Field(“EMP_DEP”)==“DEP1”&amp;&amp; r.Field(“EMP_ID”)==“EMP1”
答案 0 :(得分:0)
您可以使用lambda语法根据条件撰写查询:
IEnumerable<DataRow> query = dtInput.AsEnumerable();
if (condition1)
query = query.Where(r => r.Field<string>("EMP_DEP") == "DEP1");
if (condition2)
query = query.Where(r => r.Field<string>("EMP_ID") == "EMP1");
var whrRowEnum = query.OrderBy(r => r.Field<string>("EMP_DEP"));
另一个选择是为查询过滤器添加条件
whrRowEnum = from r in dtInput.AsEnumerable()
where (!condition1 || (r.Field<string>("EMP_DEP") == "DEP1")) &&
(!condition2 || (r.Field<string>("EMP_ID") == "EMP1"))
orderby EMP_DEP
select r;