IQueryable的简单封装

前言

前两天在园子上看到一个问题

半年前我也考虑过这些问题,但由于这样那样的问题,没有尝试去解决.

后来公司用上了 abp vnext ,然后有一部分代码可以这样写

protected override IQueryable<Resource> CreateFilteredQuery(GetResourceDto input)
{
return ReadOnlyRepository.WhereIf(
input.ParentId.HasValue,
item => item.ParentId == input.ParentId
).WhereIf(
input.Id.HasValue,
item => item.Id == input.Id
);
}

用的时候感觉还是有点方便的,但没怎么考虑过如何实现,也就这样一直用着了.

看到上面那个问题之后就想起来了这茬儿.

第一阶段

众所周知,去掉 if 的最简单的方式就是用三元 ()?():()

param = string.IsNullOrEmpty(x.CustomerID)
?param
:param.And(x => x.CustomerID == query.CustomerID);

讲道理,去掉了 if ,但是并没有感觉比用 if 的时候好多少

第二阶段

众所周知,觉得代码不好看(冗余多)的时候,你应该做点封装了

一般 来说,查询时用的应该是 IQueryable<T> Where(......)

所以可以对 IQueryable<T> 封装一些 简单 的扩展方法

public static class QueryClass
{
public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool flag, Expression<Func<T, bool>> expression)
{
return flag ? query.Where(expression) : query;
} public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, string flag, Expression<Func<T, bool>> expression)
{
return string.IsNullOrEmpty(flag) ? query : query.Where(expression);
}
}

用的时候就比较简单了

先定义一下(数据库)实体

public class Entity
{
// 可为空
public Guid? Id { get; set; }
// 字符串
public string Name { get; set; }
// 值类型
public int Num { get; set; }
}

以上应该是程序里面用到的最多的3种数据类型了(大概)

然后整点数据

List<Entity> list = new()
{
new Entity { Id = Guid.NewGuid(), Name = "2" },
new Entity { Id = Guid.NewGuid(), Name = "233333" },
new Entity { Id = Guid.NewGuid(), Name = "233333", Num = 233333 },
new Entity { Id = Guid.NewGuid(), Name = "233333", Num = 3 },
new Entity { Id = Guid.NewGuid(), Name = "23" },
......
......
new Entity { Id = Guid.NewGuid(), Name = "23", Num = 2333 },
};

然后前端传了点数据过来

Entity input = new()
{
Id = null,
Name = "233",
Num = 233
};

写条件的时候大概就像下面这样

var result = list.AsQueryable()
.WhereIf(input.Id.HasValue, item => item.Id == input.Id)
.WhereIf(input.Name, item => item.Name.Contains(input.Name))
.WhereIf(input.Num > 0, item => item.Num > input.Num * 20).ToList();

感觉用的时候还是挺方便简洁的

再多做点(过度)封装应该会更好用

第三阶段

抱歉我还在第二层......

最新文章

  1. IP多媒体子系统(IP Multimedia Subsystem,IMS)
  2. HTML概况性介绍
  3. Identity标识列
  4. [ZZ]最小化不可重现的bug
  5. POJ 1743 Musical Theme (后缀数组,求最长不重叠重复子串)(转)
  6. verilog 奇数分频设计
  7. Qt之界面出现、消失动画效果(简单好用)
  8. 标签static
  9. JSP通过SmartUpload上传文件实例
  10. git删除分支
  11. RAR压缩解压命令
  12. Win7与Ubuntu双系统时卸载Ubuntu的方法
  13. 如何使用第三方webservice
  14. MYSQL安装(Linux)
  15. 数据存储之HTTP Cookie
  16. 12 SharedPreferences
  17. 《java入门第一季》之面向对象(匿名内部类)
  18. &lt;mate name=&quot;viewport&quot;&gt;移动端设置详解
  19. postman 做接口测试之学习笔记
  20. Python3学习之路~3.1 函数基本语法及特性、返回值、参数、局部与全局变量

热门文章

  1. 冲刺随笔——Day_Five
  2. 饱含辛酸开发 WPF CustomControl
  3. go语言之---数组(array)和切片(slice)
  4. 第4.6节 print、import及断言
  5. 老猿学5G扫盲贴:R15/R16中计费架构和计费原则涉及的规范文档
  6. PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件外观展示类属性elideMode、documentMode、tabBarAutoHide、tabShape介绍
  7. 第11.5节 Python正则表达式搜索任意字符匹配及元字符“.”(点)功能介绍
  8. urllib.request.urlopen(req).read().decode解析http报文报“utf-8 codec can not decode”错处理
  9. Combiner-Reduce之前处理过程
  10. LeetCode初级算法之字符串:387 字符串中的第一个唯一字符