Browse Source

update

master
wanggang 1 year ago
parent
commit
eed8f97b2f
  1. 29
      code/src/Shared/Win.Sfs.Shared/Filter/FilterExtensions.cs

29
code/src/Shared/Win.Sfs.Shared/Filter/FilterExtensions.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@ -38,6 +38,10 @@ namespace Win.Sfs.Shared.Filter
foreach (var filterCondition in filterConditionList)
{
if (!typeof(T).GetProperties().Any(o => o.Name.ToLowerInvariant() == filterCondition.Column.ToLowerInvariant()))
{
break;
}
var tempCondition = CreateLambda<T>(filterCondition);
if (condition == null)
{
@ -59,7 +63,7 @@ namespace Win.Sfs.Shared.Filter
throw new Exception($"获取筛选条件异常:{ex.Message}");
}
return condition;
return condition == null ? p => true : condition;
}
private static Expression<Func<T, bool>> CreateLambda<T>(FilterCondition filterCondition)
@ -83,33 +87,43 @@ namespace Win.Sfs.Shared.Filter
case EnumFilterAction.Equal:
expression = Expression.Lambda<Func<T, bool>>(Expression.Equal(member, constant), parameter);
break;
case EnumFilterAction.NotEqual:
expression = Expression.Lambda<Func<T, bool>>(Expression.NotEqual(member, constant), parameter);
break;
case EnumFilterAction.BiggerThan:
expression = Expression.Lambda<Func<T, bool>>(Expression.GreaterThan(member, constant), parameter);
break;
case EnumFilterAction.SmallThan:
expression = Expression.Lambda<Func<T, bool>>(Expression.LessThan(member, constant), parameter);
break;
case EnumFilterAction.BiggerThanOrEqual:
expression = Expression.Lambda<Func<T, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
break;
case EnumFilterAction.SmallThanOrEqual:
expression = Expression.Lambda<Func<T, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
break;
case EnumFilterAction.Like:
expression = GetExpressionLikeMethod<T>("Contains", filterCondition);
break;
case EnumFilterAction.NotLike:
expression = GetExpressionNotLikeMethod<T>("Contains", filterCondition);
break;
case EnumFilterAction.In:
expression = GetExpressionInMethod<T>("Contains", member.Type, filterCondition);
break;
case EnumFilterAction.NotIn:
expression = GetExpressionNotInMethod<T>("Contains", member.Type, filterCondition);
break;
default:
break;
}
@ -123,7 +137,7 @@ namespace Win.Sfs.Shared.Filter
}
/// <summary>
///
///
/// </summary>
/// <param name="propertyType"></param>
/// <param name="value"></param>
@ -144,7 +158,6 @@ namespace Win.Sfs.Shared.Filter
var enumValue = (Enum)Enum.Parse(propertyType, value, true);
constant = Expression.Constant(enumValue);
}
else
{
constant = propertyType.Name switch
@ -182,7 +195,6 @@ namespace Win.Sfs.Shared.Filter
return Expression.Lambda<Func<T, bool>>(notMethodExpression, parameterExpression);
}
private static object GetPropertyValue(Type propertyType, string value)
{
Type lstType = typeof(List<>).MakeGenericType(propertyType);
@ -211,7 +223,7 @@ namespace Win.Sfs.Shared.Filter
}
else
{
return p=>false;
return p => false;
}
}
@ -232,7 +244,6 @@ namespace Win.Sfs.Shared.Filter
}
}
private static MethodCallExpression GetListMethodExpression(string methodName, Type propertyType, string propertyName, object propertyValue, ParameterExpression parameterExpression)
{
var propertyExpression = Expression.Property(parameterExpression, propertyName); //p.GUID
@ -242,7 +253,6 @@ namespace Win.Sfs.Shared.Filter
return Expression.Call(someValue, method, propertyExpression);
}
/// <summary>
/// 生成类似于p=>p.Code.Contains("xxx");的lambda表达式
/// parameterExpression标识p,propertyName表示values,propertyValue表示"Code",methodName表示Contains
@ -309,6 +319,5 @@ namespace Win.Sfs.Shared.Filter
var inv = Expression.Invoke(condition, exp.Parameters);
return Expression.Lambda<Func<T, bool>>(Expression.And(exp.Body, inv), exp.Parameters);
}
}
}
}

Loading…
Cancel
Save