c# - 如何按日期的一部分动态过滤
问题描述
如果我想在动态查询中构建特定规则,其中部分条件声明
一年中的同一个月
我有以下数据源:
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()
或等效的方法来做类似的事情。
解决方案
这应该很容易与动态 LINQ 一起工作:
var langExp = "ExcuseDate.Year = @0 && ExcuseDate.Month = @1 && EmpNum = @2";
var query1 = li.AsQueryable().Where(langExp, 2019, 5, 3333);
推荐阅读
- asp.net - 如何将我的 ASP.NET 应用程序与 Epicor ERP 连接?
- matplotlib - 根据 x 值在 axhline 中指定 xmax
- php - 使用 xampp 访问 localhost 上的 html/php 文件时出现错误 404
- r - 从 R 中的 dlmSvd2var 函数(dlm 包)获得的 mse 矩阵是什么?
- flutter - 未来建造者状态
- >#a0d90):I/flutter(20935):RangeError(索引):无效值:有效值范围为空:0
- java - 使用 Mockito 模拟抽象类中的静态方法
- vhdl - VHDL 端口外部名称和过程模式
- javascript - react-native modal 官方示例不适用于 react-native-web,我该如何解决?
- django - Django计算ORM中行之间的差异
- php - Laravel - 将 RAW 查询转换为 Eloquent 查询(更新查询)