我不是试图改变现有的where子句,而是将其重构为:
from a in things where a.Id == b.Id where a.Name == b.Name where a.Value1 == b.Value1 where a.Value2 == b.Value2 where a.Value3 == b.Value3 select a;
然后变成:
things.Where(a => a.Id == b.Id) .Where(a => a.Name == b.Name) .Where(a => a.Value1 == b.Value1) .Where(a => a.Value2 == b.Value2) .Where(a => a.Value1 == b.Value3);
现在应该相当清楚如何继续 - 将调用条件化为Where:
IQueryablequery = things; if (useId) { query = query.Where(a => a.Id == b.Id); } query = query.Where(a => a.Name == b.Name); if (checkValue1) { query = query.Where(a => a.Value1 == b.Value1); } // etc