c# Lambda操作類封裝

薛定諤家的貓發表於2018-10-01
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
    }
}

 

相關文章