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)
            {
                if (m.GetParameters().Count() == 2)
                {
                    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
    }
}

 

转载于:https://www.cnblogs.com/feizianquan/p/9734390.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值