using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
namespace WIDESEA_Core.Utilities
{
public static class LambdaExtensions
{
///
///
///
///
/// 字段名
/// 表达式的值
/// 创建表达式的类型,如: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 = null;
PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
if (propertyInfo != null)
proType = propertyInfo.PropertyType;
else
{
propertyInfo = typeof(T).GetProperty(propertyName.FirstLetterToLower());
if (propertyInfo != null)
proType = propertyInfo.PropertyType;
else
{
propertyInfo = typeof(T).GetProperty(propertyName.FirstLetterToUpper());
if (propertyInfo != null)
proType = propertyInfo.PropertyType;
else
{
throw new Exception($"未找到该属性,type:{typeof(T)}");
}
}
}
//创建节点变量如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