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操作类封装的更多相关文章

  1. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  2. 基于 Aspose.Cells与XML导入excel 数据----操作类封装

    前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...

  3. php-redis 操作类 封装

    <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...

  4. 【PHP+Redis】 php-redis 操作类 封装

    <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...

  5. websql操作类封装

    在之前,我写了一个websql的封装类库,代码如下: (function(win) { function smpWebSql(options){ options = options || {}; th ...

  6. [No0000DE]C# XmlHelper XML类型操作 类封装

    using System; using System.Data; using System.IO; using System.Text; using System.Threading; using S ...

  7. dapper 操作类封装

    using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using S ...

  8. [No0000DC]C# FileHelper 本地文件、文件夹操作类封装FileHelper

    using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...

  9. Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)

    今天在开发一个简单查询时,发现我的Lambda操作类的GetValue方法无法正确获取枚举类型值,以至查询结果错误. 我增加了几个单元测试来捕获错误,代码如下. /// <summary> ...

随机推荐

  1. appium----【已解决】【Mac】环境配置提示“Xcode Command Line Tools are NOT installed!"

    报错问题提示截图如下: 报错原因 :根据给出的信息很明显可以看到是"Xcode Command Line Tools"此工具没有安装 解决措施: 打开终端直接执行:xcode-se ...

  2. Python+Appium 查找 toast 方法的封装

    使用场景:在操作应用时常见toast弹框,通过toast弹框信息的获取判断当前的某个操作是否成功 引用的包:from selenium.webdriver.support import expecte ...

  3. ansible命令应用基础

    ansible命令应用基础:    Usage: ansible <host-pattern> [-f forks] [-m module_name][-a args]        -f ...

  4. PHP过滤数组中的空值

    php对数组的操作已经很完善了,提供给我们很多内置函数用以操作数组,其实可以用array_filter函数对PHP数组中的控制进行过滤 array_filter() 函数用回调函数过滤数组中的值.该函 ...

  5. go语言调度器源代码情景分析之四:函数调用栈

    本文是<go调度器源代码情景分析>系列 第一章 预备知识的第3小节. 什么是栈 栈是一种“后进先出”的数据结构,它相当于一个容器,当需要往容器里面添加元素时只能放在最上面的一个元素之上,需 ...

  6. 前端 SPA 单页应用数据统计解决方案 (ReactJS / VueJS)

    前端 SPA 单页应用数据统计解决方案 (ReactJS / VueJS) 一.百度统计的代码: UV PV 统计方式可能存在问题 在 SPA 的前端项目中 数据统计,往往就是一个比较麻烦的事情,Re ...

  7. NET微信公众号开发环境搭建(一)-了解微信由来

    公众号的应用,开发及调试环境搭建 花生壳要注册 需要二十多块钱 ,还要实名认证,估计要一两天才能审核通过  主要就是在 windows搭建测试环境   1.微信的应用场景   360百科微信简介 ht ...

  8. 设计模式 | 抽象工厂模式(abstract factory)

    定义: 提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类. 结构:(书中图,侵删) 这个图相对来说有一点点复杂,其实就是在工厂方法模式的基础上做了一些扩展,工厂方法模式只用于生成一种 ...

  9. 小程序 wepy框架 + iview-weapp的用法

    最近在弄wepy的时候在想有没有什么ui比较合适一点的wepy的,也是在网上看了好久发现iview还不错.引用简单,上手超快,组件绚丽!当然,这里还介绍下微信官方建议的框架也是和不错的,有需要的可以看 ...

  10. 安卓开发笔记(十八):实现button按钮事件的三种方法

    Android开发中有三种主要的方式用于设置View的点击事件,1.创建内部类:2.主类中实现OnClickListener接口:3.使用匿名内部类.这三种方式都用到了OnClickListener接 ...