经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » SQL语言 » 查看文章
怎样优雅地增删查改(九):按日期范围查询
来源:cnblogs  作者:林晓lx  时间:2023/7/24 8:53:22  对本文有异议

使用数据库的创建时间作为查询依据,在Abp框架中,实体类实现ICreationAuditedObject接口,或继承CreationAuditedEntity类,使用仓储创建记录时将自动生成CreationTime。

实现

定义按创建日期范围查询(IDateSpanOrientedFilter)接口。

遵守接口隔离原则,将IDateSpanOrientedFilter接口拆分为IStartDateOrientedFilter和IEndDateOrientedFilter接口。

  1. public interface IDateSpanOrientedFilter : IStartDateOrientedFilter, IEndDateOrientedFilter
  2. {
  3. }

按开始日期查询接口定义如下:

  1. public interface IStartDateOrientedFilter
  2. {
  3. DateTime? StartDate { get; set; }
  4. }

结束日期接口定义如下:

  1. public interface IEndDateOrientedFilter
  2. {
  3. DateTime? EndDate { get; set; }
  4. }
  • StartDate:开始日期,记录的CreationTime 大于等于 该日期的记录将被筛选
  • EndDate:用户Id,记录的CreationTime 小于 该日期的记录将被筛选

按开始日期查询

创建应用过滤条件方法:ApplyStartDateOrientedFiltered,在此实现拼接LINQ表达式,代码如下:

  1. protected virtual IQueryable<TEntity> ApplyStartDateOrientedFiltered(IQueryable<TEntity> query, TGetListInput input)
  2. {
  3. if (input is IStartDateOrientedFilter && HasProperty<TEntity>("CreationTime"))
  4. {
  5. var property = typeof(TEntity).GetProperty("CreationTime");
  6. var filteredInput = input as IStartDateOrientedFilter;
  7. if (filteredInput != null && filteredInput.StartDate.HasValue)
  8. {
  9. Expression originalExpression = null;
  10. var parameter = Expression.Parameter(typeof(TEntity), "p");
  11. var dateConstantExpression = Expression.Constant(filteredInput.StartDate.Value, typeof(DateTime));
  12. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  13. var expression = Expression.GreaterThanOrEqual(propertyAccess, dateConstantExpression);
  14. var equalExpression = expression != null ?
  15. Expression.Lambda<Func<TEntity, bool>>(expression, parameter)
  16. : p => false;
  17. query = query.Where(equalExpression);
  18. }
  19. }
  20. return query;
  21. }

按结束日期查询

创建应用过滤条件方法:ApplyEndDateOrientedFiltered,在此实现拼接LINQ表达式,代码如下:

  1. protected virtual IQueryable<TEntity> ApplyEndDateOrientedFiltered(IQueryable<TEntity> query, TGetListInput input)
  2. {
  3. if (input is IEndDateOrientedFilter && HasProperty<TEntity>("CreationTime"))
  4. {
  5. var property = typeof(TEntity).GetProperty("CreationTime");
  6. var filteredInput = input as IEndDateOrientedFilter;
  7. if (filteredInput != null && filteredInput.EndDate.HasValue)
  8. {
  9. Expression originalExpression = null;
  10. var parameter = Expression.Parameter(typeof(TEntity), "p");
  11. var dateConstantExpression = Expression.Constant(filteredInput.EndDate.Value, typeof(DateTime));
  12. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  13. var expression = Expression.LessThan(propertyAccess, dateConstantExpression);
  14. var equalExpression = expression != null ?
  15. Expression.Lambda<Func<TEntity, bool>>(expression, parameter)
  16. : p => false;
  17. query = query.Where(equalExpression);
  18. }
  19. }
  20. return query;
  21. }

请注意,可应用过滤的条件为:

  1. input需实现IDateSpanOrientedFilter或子接口;
  2. 实体必须包含“CreationTime”字段。

否则将原封不动返回IQueryable对象。

使用

无需在应用层中更改代码,

在GetAllAlarmInput中实现IDateSpanOrientedFilter接口,代码如下:

  1. public class GetAllAlarmInput : PagedAndSortedResultRequestDto, IDateSpanOrientedFilter
  2. {
  3. public DateTime? StartDate { get; set; }
  4. public DateTime? EndDate { get; set; }
  5. ...
  6. }

至此,所有的通用查询接口已实现完成。在这个项目中,我实现了适合我的联合查询方式,你可以根据实际业务需求,扩展和调整查询实现。

项目地址

Github:general-curd-sample

原文链接:https://www.cnblogs.com/jevonsflash/p/17574382.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号