c# Lambda操作类封装
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace EasyFrame.Common
{
public static class LambdaCommon
{
#region 表达式工具
/// <summary>
/// 相当于&&操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisFilter">已生成的过滤条件</param>
/// <param name="otherFilter">未生成的过滤条件</param>
/// <returns>新的过滤</returns>
public static Expression GotoAndAlso(this Expression thisFilter, Expression otherFilter)
{
return Expression.AndAlso(thisFilter, otherFilter);
}
/// <summary>
/// 相当于||操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisFilter">已生成的过滤条件</param>
/// <param name="otherFilter">未生成的过滤条件</param>
/// <returns>新的过滤</returns>
public static Expression GotoOrElse(this Expression thisFilter, Expression otherFilter)
{
return Expression.OrElse(thisFilter, otherFilter);
}
/// <summary>
/// 相当于==操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
return Expression.Equal(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
}
/// <summary>
/// 相当于>=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanOrEqual<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于或等于
return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于小于等于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThanOrEqual<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于或等于
return Expression.LessThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于!=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoNotEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
return Expression.NotEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
}
/// <summary>
/// 相当于>=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanOrEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于或等于
return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
}
/// <summary>
/// 相当于>操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThan<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于
return Expression.GreaterThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于小于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThan<T>(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于
return Expression.LessThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
}
/// <summary>
/// 相当于>=操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanOrEqualByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于或等于
return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 字符串包含
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoContains(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
return Expression.Call(Expression.Property(thisParameterExpression, propertieName), typeof(string).GetMethod("Contains"), Expression.Constant(propertieValue));
}
/// <summary>
/// 相当于小于或等于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThanOrEqualByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于或等于
return Expression.LessThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 相当于>操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoGreaterThanByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//大于
return Expression.GreaterThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 相当于小于操作
/// ——Author:hellthinker
/// </summary>
/// <param name="thisParameterExpression">查询对象</param>
/// <param name="propertieName">属性名称</param>
/// <param name="propertieValue">属性值</param>
/// <returns>新的过滤</returns>
public static Expression GotoLessThanByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
//小于
return Expression.LessThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
}
/// <summary>
/// 包含操作 相当余 a=> arr.Contains(a.ID)
/// </summary>
/// <param name="thisParameterExpression"></param>
/// <param name="propertieName"></param>
/// <param name="propertieValue"></param>
/// <returns></returns>
public static Expression ContainsOperations(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
MethodInfo method = null;
MemberExpression member = Expression.Property(thisParameterExpression, propertieName);
var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(m => m.Name == "Contains");
foreach (var m in containsMethods)
{
)
{
method = m;
break;
}
}
method = method.MakeGenericMethod(member.Type);
var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(propertieValue), member });
return exprContains;
}
/// <summary>
/// 包含操作 相当于 a=>a.ID.Contains(key)
/// </summary>
/// <param name="thisParameterExpression"></param>
/// <param name="propertieName"></param>
/// <param name="propertieValue"></param>
/// <returns></returns>
public static Expression Contains(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
{
var propertyExp = Expression.Property(thisParameterExpression, propertieName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
var someValue = Expression.Constant(propertieValue, typeof(string));
var containsMethodExp = Expression.Call(propertyExp, method, someValue);
return containsMethodExp;
}
#endregion
}
}
c# Lambda操作类封装的更多相关文章
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- 基于 Aspose.Cells与XML导入excel 数据----操作类封装
前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...
- php-redis 操作类 封装
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- 【PHP+Redis】 php-redis 操作类 封装
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- websql操作类封装
在之前,我写了一个websql的封装类库,代码如下: (function(win) { function smpWebSql(options){ options = options || {}; th ...
- [No0000DE]C# XmlHelper XML类型操作 类封装
using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...
- dapper 操作类封装
using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using S ...
- [No0000DC]C# FileHelper 本地文件、文件夹操作类封装FileHelper
using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...
- Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)
今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...
随机推荐
- Appium+python的环境搭建!
我的电脑64位操作系统 下载并安装所需软件: 1.adb环境 2.下载(python-2.7.12.amd64.msi)并安装python 3.下载并解压adt,如(adt-bundle-window ...
- 计算机17-3,4作业F
F.complete number problem with formatted output Description 同题目E Input N Output complete numbers w ...
- mysql服务设置远程连接 解决1251 client does not support ..问题
在docker里面创建mysql容器后设置的密码在远程主机连接时候出现错误: 一.如果是在docker里面安装的mysql镜像则需要先进入mysql里面:参考上一篇:https://www.cnblo ...
- Promise原理—一步一步实现一个Promise
promise特点 一个promise的当前状态只能是pending.fulfilled和rejected三种之一.状态改变只能是pending到fulfilled或者pending到rejected ...
- Layer 使用
官网文档 http://layer.layui.com/mobile/api.html 1.需要添加jquery的引用然后是 loadExtentFile("css", " ...
- STM32学习笔记(一):跑马灯
本实验所采用的开发板为正点原子的MiniSTM32f103rc开发板,主函数程序如下,注释如下:main.c #include "stm32f10x.h" void Delay(u ...
- SpringBoot SpEL表达式注入漏洞-分析与复现
目录 0x00前言 0x01触发原因 0x02调试分析 0x03补丁分析 0x04参考文章 影响版本: 1.1.0-1.1.12 1.2.0-1.2.7 1.3.0 修复方案:升至1.3.1或以上版本 ...
- Google 的 QUIC 华丽转身成为下一代网络协议: HTTP/3.0
HTTP/2.0 还没有普及,HTTP/3.0 标准就要被制定了. 据 IETF 透露,HTTP-over-QUIC 实验协议将被重命名为 HTTP/3,并成为 HTTP 协议的第三个正式版本. IE ...
- Caffe源码理解3:Layer基类与template method设计模式
目录 写在前面 template method设计模式 Layer 基类 Layer成员变量 构造与析构 SetUp成员函数 前向传播与反向传播 其他成员函数 参考 博客:blog.shinelee. ...
- Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 本篇博客我们讲解下在Spring Boot中使用MyBatis访问MySql数据库的简单用法. 1.前期 ...