Browse Source

update

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

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

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