using Common.Const;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Common.Extensions
{
public static class LambdaExtensions
{
///
/// 分页查询
///
///
///
///
///
///
public static IQueryable TakePage(this IQueryable queryable, int page, int size = 15)
{
return queryable.TakeOrderByPage(page, size);
}
///
/// 分页查询
///
///
///
///
///
///
///
public static IQueryable TakeOrderByPage(this IQueryable queryable, int page, int size = 15, Expression>> orderBy = null)
{
if (page <= 0)
{
page = 1;
}
return Queryable.Take(Queryable.Skip(queryable.GetIQueryableOrderBy(orderBy.GetExpressionToDic()), (page - 1) * size), size);
}
///
/// 创建lambda表达式:p=>true
///
///
///
public static Expression> True()
{
return p => true;
}
///
/// 创建lambda表达式:p=>false
///
///
///
public static Expression> False()
{
return p => false;
}
public static ParameterExpression GetExpressionParameter(this Type type)
{
return Expression.Parameter(type, "p");
}
///
/// 创建lambda表达式:p=>p.propertyName
///
///
///
///
///
public static Expression> GetExpression(this string propertyName)
{
return propertyName.GetExpression(typeof(T).GetExpressionParameter());
}
///
/// 创建委托有返回值的表达式:p=>p.propertyName
///
///
///
///
///
public static Func GetFun(this string propertyName)
{
return propertyName.GetExpression(typeof(T).GetExpressionParameter()).Compile();
}
///
/// 创建lambda表达式:p=>false
/// 在已知TKey字段类型时,如动态排序OrderBy(x=>x.ID)会用到此功能,返回的就是x=>x.ID
/// Expression> expression = x => x.CreateDate;指定了类型
///
///
///
public static Expression> GetExpression(this string propertyName, ParameterExpression parameter)
{
if (typeof(TKey).Name == "Object")
return Expression.Lambda>(Expression.Convert(Expression.Property(parameter, propertyName), typeof(object)), parameter);
return Expression.Lambda>(Expression.Property(parameter, propertyName), parameter);
}
///
/// 创建lambda表达式:p=>false
/// object不能确认字段类型(datetime,int,string),如动态排序OrderBy(x=>x.ID)会用到此功能,返回的就是x=>x.ID
/// Expression> expression = x => x.CreateDate;任意类型的字段
///
///
///
public static Expression> GetExpression(this string propertyName)
{
return propertyName.GetExpression(typeof(T).GetExpressionParameter());
}
public static Expression> GetExpression(this string propertyName, ParameterExpression parameter)
{
return Expression.Lambda>(Expression.Convert(Expression.Property(parameter, propertyName), typeof(object)), parameter);
}
///
///
///
///
/// 字段名
/// 表达式的值
/// 创建表达式的类型,如:p=>p.propertyName != propertyValue
/// p=>p.propertyName.Contains(propertyValue)
///
public static Expression> CreateExpression(this string propertyName, object propertyValue, LinqExpressionType expressionType)
{
return propertyName.CreateExpression(propertyValue, null, expressionType);
}
///
///
///
///
/// 字段名
/// 表达式的值
/// 创建表达式的类型,如:p=>p.propertyName != propertyValue
/// p=>p.propertyName.Contains(propertyValue)
///
private static Expression> CreateExpression(
this string propertyName,
object propertyValue,
ParameterExpression parameter,
LinqExpressionType expressionType)
{
Type proType = typeof(T).GetProperty(propertyName).PropertyType;
//创建节点变量如p=>的节点p
// parameter ??= Expression.Parameter(typeof(T), "p");//创建参数p
parameter = parameter ?? Expression.Parameter(typeof(T), "p");
//创建节点的属性p=>p.name 属性name
MemberExpression memberProperty = Expression.PropertyOrField(parameter, propertyName);
if (expressionType == LinqExpressionType.In)
{
if (!(propertyValue is System.Collections.IList list) || list.Count == 0) throw new Exception("属性值类型不正确");
bool isStringValue = true;
List