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> ...
随机推荐
- 本地和svn都删除文件导致版本不同的问题
想要删除一个项目中的文件,同是要删除svn上的文件. 自己操作 1.直接右键删除了本地项目中的一个目录的模块 2.右键删除了库中svn中的这个目录 3.同步本地和svn上的代码 4.问题出现了,本地和 ...
- Docker 配置国内镜像加速器,加速下载速度
文章首发自个人微信公众号:小哈学Java 个人网站地址:https://www.exception.site/docker/docker-configuration-of-mirror-acceler ...
- 女朋友也能看懂的Zookeeper分布式锁原理
前言 关于分布式锁,在互联网行业的使用场景还是比较多的,比如电商的库存扣减,秒杀活动,集群定时任务执行等需要进程互斥的场景.而实现分布式锁的手段也很多,大家比较常见的就是redis跟zookeep ...
- CSS消除button标签的默认样式
button{ /*消除button的默认样式*/ /*这种写法是对所有的button标签同时生效*/ margin: 0px; padding: 0px; /*自定义边框*/ border: 0px ...
- Android Service与Activity的交互
Android中有时候需要在Service中改变Activity的UI,或者在Activity中修改Service中的数值.首先必须使用与Activity绑定的Service,有三种方式可以实现.第一 ...
- android 自定义权限管理
在Android6.0后有些权限就需要进行询问,虽然可以将targetSdkVersion设置成小于等于23,但是这样可能有些东西无法使用,所以要进行权限的管理. 实现逻辑:打开页面就询问权限,如果没 ...
- ORA-39127: 调用 "WMSYS"."LT_EXPORT_PKG"."SCHEMA_INFO_EXP" 时发生意外错误
expdp 告警提示: Export: Release 11.2.0.4.0 - Production on 星期日 4月 28 12:14:51 2019....ORA-39127: 调用 &quo ...
- Liunx-cd命令
1. 如何进入上级目录cd .. 2. 如何进入当前用户主目录cd ~3. 如何进入上两级目录cd ../.. 4. 进入当前目录命令cd .5. 如何进入目录 /lym/b 6.切换跟目录
- 如何用RSS订阅?
本文由云+社区发表 摘要:我们常常会有订阅别人文章的需求,有更新的时候希望能有提醒的功能,RSS就是这样一个订阅的方式.很多网站上看到RSS的入口,点进去以后总是显示一堆的XML代码,我们来看看怎么使 ...
- (转)ceph 常用 运维命令--查看信息 - 不错的文档
下面是测试验证环节 1. 创建一个 pool rbd create foo --size 4 --image-format 2 --image-feature layering 2. 挂载和格式化 r ...