我正在尝试在condition
为true的情况下执行以下操作,我想执行WHERE
,否则不行。
var condition = true;
var mast = new List<Master>
{
new Master{Id = 2, Prop1 = "Default", Prop2 = "Data", Prop3 = 11},
new Master{Id = 3, Prop1 = "Some", Prop2 = "TestData", Prop3 = 11},
new Master{Id = 4, Prop1 = "Some", Prop2 = "MoreData", Prop3 = 11},
};
var g = mast.Where(w=> condition ? (x => x.Prop1.ToLower() != "default" || x.Prop2.ToLower() != "data") : true = true).ToList();
上面的代码给我错误,
最佳答案
只需将条件移到lambda中即可:
var g = mast.Where(w => (condition
? (w.Prop1.ToLower() != "default" || w.Prop2.ToLower() != "data")
: true)
).ToList();
关于c# - 如何在LINQ查询中放入条件WHERE子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54995561/