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. 车牌定位与畸变校正(python3.7,opencv4.0)

    一.前言及思路简析 目前车牌识别系统在各小区门口随处可见,识别效果貌似都还可以.查阅资料后,发现整个过程又可以细化为车牌定位.畸变校正.车牌分割和内容识别四部分.本篇随笔主要介绍车牌定位及畸变校正两部 ...

  2. SpringBoot进阶教程(三十)整合Redis之Sentinel哨兵模式

    Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时,假如master宕机了,redis本身(以及其很多客户端)都没有实现自动进行主备切换,而 ...

  3. 服务部署到Swarm Cluster中

    对于已存在的镜像,将其部署到服务器中并开始对外服务,便是它的职责,而我们要做的便是帮助它完成职责,前两个应用环节都已产生了相应的镜像,在这一环节,将完成服务部署到容器集群的工作,对于这一过程,实际执行 ...

  4. Vue2.0源码阅读笔记(三):计算属性

      计算属性是基于响应式依赖进行缓存的,只有在相关响应式依赖发生改变时才会重新求值,这种缓存机制在求值消耗比较大的情况下能够显著提高性能. 一.计算属性初始化   Vue 在做数据初始化时,通过 in ...

  5. 如何使用vs将asp.net core项目添加容器支持并发布docker镜像到私有dockerhub和添加k8s/helm管理

    这篇文章介绍一下,如何使用VS2017给asp.net core添加容器支持,并发布镜像到私有docker hub,然后用chart管理容器镜像的操作流程. 话不多说,just do it. 新建项目 ...

  6. 结合JDK源码看设计模式——桥接模式

    前言: 在我们还没学习框架之前,肯定都学过JDBC.百度百科对JDBC是这样介绍的[JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Jav ...

  7. 如何通过免费开源的ERP Odoo打造企业全员营销整体解决方案

    应用场景的背景故事 在一些二级城市,往往线索的来源是通过企业当地口碑积累.熟人转介绍等线下的方式为主,利用互联网的模式往往很难奏效,企业面临的第一个问题就是如何把握线索真实的来源介绍的问题.在这个问题 ...

  8. ArcPy 批量给shp字段赋值

    工作中需要做大量图层的拼接,为了在拼接完成后还能知道原始数据文件是什么,所以写了个Python脚本对每个图层的SOURCE字段进行赋值. 附上Python代码: # -*- coding: utf-8 ...

  9. 云计算CRM软件厂商,你青睐哪家?

    2018年CRM系统软件市场风起云涌,国内外厂商群雄逐鹿.2019年,新一轮的角逐已然展开.据Gartner报告称,CRM客户关系管理系统在2017年全球的销售额首次超越数据库管理系统((Databa ...

  10. 解决 win10飞行模式 无限自动开关 无法关闭

    驱动问题,名为“Insyde Airplane Mode HID Mini-Driver”的驱动,这个驱动是专门用来快捷管理飞行模式的. 卸载完成后重启,无限开关飞行模式问题得到解决!