如果我想在动态查询中建立特定的规则,其中部分条件说明该情况
一年中的同一个月
我有以下数据源:
List<Excuse> li = new List<Excuse>();
li.Add(new Excuse { EmpNum = 3333, ExcuseDate = DateTime.Today, ExcuseDuration = new TimeSpan(0, 30, 0), ExcuseType = 0, Id = 1 });
li.Add(new Excuse { EmpNum = 3333, ExcuseDate = DateTime.Today.AddDays(1), ExcuseDuration = new TimeSpan(2, 30, 0), ExcuseType = 0, Id = 2 });
li.Add(new Excuse { EmpNum = 3333, ExcuseDate = DateTime.Today.AddDays(2), ExcuseDuration = new TimeSpan(2, 0, 0), ExcuseType = 0, Id = 3 });
li.Add(new Excuse { EmpNum = 2345, ExcuseDate = DateTime.Now, ExcuseDuration = new TimeSpan(0, 30, 0), ExcuseType = 0, Id = 4});
如何写这样的东西:
var langExp = "YEAR(ExcuseDate) = @1 AND MONTH(ExcuseDate) = @2 AND emp_num = @3";
var query1 = li.AsQueryable().Where(langExp, 2019,5,3333);
我希望使用Year() or Month()
之类的方法或等效方法来做类似的事情。
答案 0 :(得分:2)
这对于Dynamic LINQ应该很容易工作:
var langExp = "ExcuseDate.Year = @0 && ExcuseDate.Month = @1 && EmpNum = @2";
var query1 = li.AsQueryable().Where(langExp, 2019, 5, 3333);