使用 Expression.Parameter 中的子属性构建 linq 表达式
Use sub properties in Expression.Parameter to build a linq expression
假设我们有以下正常表达式:
var result = SomeList.Where(item => item.Status.Description.Contains("READY"));
对于这些对象:
public class Movie
{
public MovieStatus Status {get; set;}
}
public class MovieStatus
{
public string Description {get; set;}
}
这行不通:
ParameterExpression pe = Expression.Parameter(typeof(T), "item.Status");
MemberExpression propExp = Expression.Property(pe, "Description");//boem! Description is not a property of T.
通过 T
属性的一些递归,我可以获得正确的 MemberExpression
并且在调试时它看起来不错,最后我有这个 lambda 表达式:
{item => item.Status.Description.Contains("READY")}
并且,当将这些表达式应用于 IQueryable
列表时,结果将是:
{System.Collections.Generic.List`1[Movie].Where(item => item.Status.Description.Contains("READY"))}
看起来不错,但是在编译/执行列表中的表达式时,出现以下错误:
Additional information: variable 'item.Status' of type 'MovieStatus' referenced from scope '', but it is not defined
我需要怎样处理 'chain' ParameterExpression
才能得到上面的 lambda 表达式?
真正的代码没有这些固定变量,它是一个通用实现,可以被任何具有任何子属性的对象使用。输入是正常格式XX.YY中的属性名称和一个比较值。 post 所有代码有点庞大,但下面是其中的一个子集,消除了所有递归以专注于问题。递归的一些结果已在此处进行了硬编码。此外,它并不总是 Contains
.
public static void Test<T>(IQueryable<T> source)
{
string propertyName = "Status.Description";
string value = "READY";
ParameterExpression pe = Expression.Parameter(typeof(T), "item");
Type type = typeof(T).GetProperty("Status").PropertyType;//property name is some recursion result
ParameterExpression peSub = Expression.Parameter(type, "item.Status");
MemberExpression propExp = Expression.Property(peSub, "Description");
Expression whereValue = GetValueExpression(value, type);
//do the contains rule expression
Type subType = type.GetProperty("Description").PropertyType;//property name is also recursion result
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { subType });
Expression ruleExpression = Expression.Call(propExp, containsMethod, whereValue);
//create source.Where([expressions])
Type[] elementTypes = new Type[] { source.ElementType };
Expression<Func<T, bool>> labdaExpression = Expression.Lambda<Func<T, bool>>(ruleExpression, new ParameterExpression[] { pe });
//method call expression
Expression whereCallExpression = Expression.Call(typeof(Queryable), "Where",
elementTypes, source.Expression, labdaExpression);
source = source.Provider.CreateQuery<T>(whereCallExpression);
source.ToList();//boom, error: Additional information: variable 'item.Status' of type 'MovieStatus' referenced from scope '', but it is not defined
}
您在 where
子句中只有一个参数。让我们先创建它:
public static IQueryable<T> Where<T>(this IQueryable<T> query, string selector, string comparer, string value)
{
var target = Expression.Parameter(typeof(T));
return query.Provider.CreateQuery<T>(CreateWhereClause(target, query.Expression, selector, comparer, value));
}
对于参数,我们需要创建那个子句,它实际上是一个调用表达式,"quotes" 实际的 lambda:
static Expression CreateWhereClause(ParameterExpression target, Expression expression, string selector, string comparer, string value)
{
var predicate = Expression.Lambda(CreateComparison(target, selector, comparer, value), target);
return Expression.Call(typeof(Queryable), nameof(Queryable.Where), new[] { target.Type },
expression, Expression.Quote(predicate));
}
lambda 表达式应该包含实际比较,其中包含左侧的成员访问权限和右侧的实际值:
static Expression CreateComparison(ParameterExpression target, string selector, string comparer, string value)
{
var memberAccess = CreateMemberAccess(target, selector);
var actualValue = Expression.Constant(value, typeof(string));
return Expression.Call(memberAccess, comparer, null, actualValue);
}
对于成员访问,我们可以链接这些 属性 表达式:
static Expression CreateMemberAccess(Expression target, string selector)
{
return selector.Split('.').Aggregate(target, (t, n) => Expression.PropertyOrField(t, n));
}
最后,您应该能够:
query.Where("Status.Description", "Contains", "READY");
顺便说一句,我只是简化了 this code,希望能提供相应的答案。
假设我们有以下正常表达式:
var result = SomeList.Where(item => item.Status.Description.Contains("READY"));
对于这些对象:
public class Movie
{
public MovieStatus Status {get; set;}
}
public class MovieStatus
{
public string Description {get; set;}
}
这行不通:
ParameterExpression pe = Expression.Parameter(typeof(T), "item.Status");
MemberExpression propExp = Expression.Property(pe, "Description");//boem! Description is not a property of T.
通过 T
属性的一些递归,我可以获得正确的 MemberExpression
并且在调试时它看起来不错,最后我有这个 lambda 表达式:
{item => item.Status.Description.Contains("READY")}
并且,当将这些表达式应用于 IQueryable
列表时,结果将是:
{System.Collections.Generic.List`1[Movie].Where(item => item.Status.Description.Contains("READY"))}
看起来不错,但是在编译/执行列表中的表达式时,出现以下错误:
Additional information: variable 'item.Status' of type 'MovieStatus' referenced from scope '', but it is not defined
我需要怎样处理 'chain' ParameterExpression
才能得到上面的 lambda 表达式?
真正的代码没有这些固定变量,它是一个通用实现,可以被任何具有任何子属性的对象使用。输入是正常格式XX.YY中的属性名称和一个比较值。 post 所有代码有点庞大,但下面是其中的一个子集,消除了所有递归以专注于问题。递归的一些结果已在此处进行了硬编码。此外,它并不总是 Contains
.
public static void Test<T>(IQueryable<T> source)
{
string propertyName = "Status.Description";
string value = "READY";
ParameterExpression pe = Expression.Parameter(typeof(T), "item");
Type type = typeof(T).GetProperty("Status").PropertyType;//property name is some recursion result
ParameterExpression peSub = Expression.Parameter(type, "item.Status");
MemberExpression propExp = Expression.Property(peSub, "Description");
Expression whereValue = GetValueExpression(value, type);
//do the contains rule expression
Type subType = type.GetProperty("Description").PropertyType;//property name is also recursion result
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { subType });
Expression ruleExpression = Expression.Call(propExp, containsMethod, whereValue);
//create source.Where([expressions])
Type[] elementTypes = new Type[] { source.ElementType };
Expression<Func<T, bool>> labdaExpression = Expression.Lambda<Func<T, bool>>(ruleExpression, new ParameterExpression[] { pe });
//method call expression
Expression whereCallExpression = Expression.Call(typeof(Queryable), "Where",
elementTypes, source.Expression, labdaExpression);
source = source.Provider.CreateQuery<T>(whereCallExpression);
source.ToList();//boom, error: Additional information: variable 'item.Status' of type 'MovieStatus' referenced from scope '', but it is not defined
}
您在 where
子句中只有一个参数。让我们先创建它:
public static IQueryable<T> Where<T>(this IQueryable<T> query, string selector, string comparer, string value)
{
var target = Expression.Parameter(typeof(T));
return query.Provider.CreateQuery<T>(CreateWhereClause(target, query.Expression, selector, comparer, value));
}
对于参数,我们需要创建那个子句,它实际上是一个调用表达式,"quotes" 实际的 lambda:
static Expression CreateWhereClause(ParameterExpression target, Expression expression, string selector, string comparer, string value)
{
var predicate = Expression.Lambda(CreateComparison(target, selector, comparer, value), target);
return Expression.Call(typeof(Queryable), nameof(Queryable.Where), new[] { target.Type },
expression, Expression.Quote(predicate));
}
lambda 表达式应该包含实际比较,其中包含左侧的成员访问权限和右侧的实际值:
static Expression CreateComparison(ParameterExpression target, string selector, string comparer, string value)
{
var memberAccess = CreateMemberAccess(target, selector);
var actualValue = Expression.Constant(value, typeof(string));
return Expression.Call(memberAccess, comparer, null, actualValue);
}
对于成员访问,我们可以链接这些 属性 表达式:
static Expression CreateMemberAccess(Expression target, string selector)
{
return selector.Split('.').Aggregate(target, (t, n) => Expression.PropertyOrField(t, n));
}
最后,您应该能够:
query.Where("Status.Description", "Contains", "READY");
顺便说一句,我只是简化了 this code,希望能提供相应的答案。