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. 腾讯面试:一条SQL语句执行得很慢的原因有哪些?---不看后悔系列

    说实话,这个问题可以涉及到 MySQL 的很多核心知识,可以扯出一大堆,就像要考你计算机网络的知识时,问你"输入URL回车之后,究竟发生了什么"一样,看看你能说出多少了. 之前腾讯 ...

  2. 关于Redis的常见面试题解析

    1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...

  3. vue.js框架原理浅析

    vue.js是一个非常优秀的前端开发框架,不是我说的,大家都知道. 首先我现在的能力,独立阅读源码还是有很大压力的,所幸vue写的很规范,通过方法名基本可以略知一二,里面的原理不懂的地方多方面查找资料 ...

  4. 如何使用FluentMigrator进行数据库迁移

    标题:如何使用FluentMigrator进行数据库迁移 地址:https://www.cnblogs.com/lwqlun/p/10649949.html 作者: Lamond Lu FluentM ...

  5. PHP 技能精进之 PHP-FPM 多进程模型

    PHP-FPM 提供了更好的 PHP 进程管理方式,可以有效控制内存和进程.可以平滑重载PHP配置.那么当我们谈论 PHP-FPM 多进程模型的时候,作为 PHPer 的你了解多少呢? 首先,让我们一 ...

  6. 【Java】广州三本秋招经历

    前言 只有光头才能变强 离上次发文章已经快两个月时间了,最近一直忙着秋招的事.今天是2018年10月22日,对于互联网行业来说,秋招就基本结束了.我这边的流程也走完了(不再笔试/面试了),所以来写写我 ...

  7. 微信公众号开发C#系列-11、生成带参数二维码应用场景

    1.概述 我们在微信公众号开发C#系列-7.消息管理-接收事件推送章节有对扫描带参数二维码事件的处理做了讲解.本篇主要讲解通过微信公众号开发平台提供的接口生成带参数的二维码及应用场景. 微信公众号平台 ...

  8. 如何在CentOS上创建Kubernetes集群

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由编程男孩 发表于云+社区专栏 介绍 Kubernetes(常简称为K8s)是用于自动部署.扩展和管理容器化(containerized ...

  9. 底部导航栏-----FragmentTabHost

    [说明] 1.主界面上添加父容器:FragmentTabHost 属于v4兼容包 需要指定该id为android:id/tabhost,不能修改,表示由android系统来托管这个id. 本身是一个F ...

  10. mssql sqlserver isnull coalesce函数用法区别说明

    摘要: 下文讲述isnull及coalesce空值替换函数的区别 isnull.coalesce函数区别:1.isnull 只能接受两个参数,而coalesce函数可以接受大于等于两个以上参数2.is ...