我试图根据存储在另一个dbset中的属性进行一些文章筛选。我正在使用一些课程:
public class Article
{
public string ArticleCode { get; set; }
public string var1 { get; set; }
public string var2 { get; set; }
public string var3 { get; set; }
public virtual List<ArticleProperty> Properties { get; set; }
}
public class ArticleProperty
{
public string ArticleCode { get; set; }
public string PropertyCode { get; set; }
public string var4 { get; set; }
public string var5 { get; set; }
public string var6 { get; set; }
}
public class ArticleSummary
{
public string ArticleCode { get; set; }
public string var7 { get; set; }
public string var8 { get; set; }
}
public class WebDbContext : DbContext
{
public virtual DbSet<Article> Article{ get; set; }
public virtual DbSet<ArticleProperty> ArticleProperty{ get; set; }
/* some more code */
}
当我创建这样的查询时,它会执行我想做的事情:
IQueryable<ArticleSummary> Articles = _DbContext.Article
.Where(a => a.var1 == SomeLocalVariable1)
.Where(a => a.var2 == SomeLocalVariable2 || a.var2 == SomeLocalVariable3)
.Where(a =>
a.Properties.Any(ap =>
(
(ap.ArticleCode == a.ArticleCode && ap.var4 == "A" && ap.var5 == "X") ||
(ap.ArticleCode == a.ArticleCode && ap.var4 == "A" && ap.var5 == "Y")
)
)
&&
a.Eigenschappen.Any(ap =>
(
(ap.ArticleCode == a.ArticleCode && ap.var4 == "B" && ap.var5 == "Z")
)
)
)
.OrderByDescending(a => a.var6)
.Select(a => new ArticleSummary
{
ArticleCode = a.ArticleCode ,
var7 = a.var1
var8 = a.var3
});
但是现在我想动态创建最后一个Where语句,像这样(dataFilter是一个Dictionary <字符串,Dictionary <字符串,bool >>以及一些过滤器属性):
var query ="";
bool firstA = true;
foreach (KeyValuePair<string, Dictionary<string, bool>> filter in dataFilter)
{
if (firstA)
query += "a => ";
else
query += " && ";
query += "a.Properties.Any(ap =>"
+ "(";
bool firstB = true;
foreach (KeyValuePair<string,bool> filterDetail in filter.Value)
{
if (!firstB)
query += " || ";
query += "(ap.ArticleCode == a.ArticleCode && ap.var4 == \""+filter.Key+"\" && ap.var5 == \""+filterDetail.Key+"\")";
firstB = false;
}
query += ")"
+ ")";
firstA = false;
}
IQueryable<ArticleSummary> Articles = _DbContext.Article
.Where(a => a.var1 == SomeLocalVariable1)
.Where(a => a.var2 == SomeLocalVariable2 || a.var2 == SomeLocalVariable3)
.Where(query)
.OrderByDescending(a => a.var6)
.Select(a => new ArticleSummary
{
ArticleCode = a.ArticleCode ,
var7 = a.var1
var8 = a.var3
});
“查询”符合预期,但“位置”不起作用,错误:
System.Linq.Dynamic.Core.Exceptions.ParseException: 'No applicable aggregate method 'Any' exists'
这仅在有2个“ Any”-语句(用&&除,与我对其进行“硬编码”时相同)时发生。我不知道为什么...
答案 0 :(得分:1)
动态LINQ有自己的expression language。 Lambda表达式不是以a =>
或ap =>
开头,存在一种称为“当前范围”的东西,它简化了一些查询,但通常在访问外部级参数方面存在问题。所有可查询的扩展都定义了一个称为it
的单个范围参数,可以省略。
简短地说,动态LINQ不太适合嵌套lambda表达式访问外部lambda参数的复杂查询。
结合编译时间和运行时表达式可以相对容易地实现目标。这个想法很简单。
首先,您将创建一个带有附加参数(用作占位符)的编译时lambda表达式。然后,使用以下简单的表达式访问者将占位符替换为实际表达式:
public static class ExpressionExtensions
{
public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target)
{
return new ParameterReplacer { Source = source, Target = target }.Visit(expression);
}
class ParameterReplacer : ExpressionVisitor
{
public ParameterExpression Source;
public Expression Target;
protected override Expression VisitParameter(ParameterExpression node)
=> node == Source ? Target : base.VisitParameter(node);
}
}
非常类似于string.Format
,但带有表达式。然后,您可以使用Expression.AndAlso
和Expression.OrElse
来生成&&
和||
的部分。
话虽如此,这就是您的情况:
Expression<Func<Article, string, string, bool>> detailExpr = (a, var4, var5) =>
a.Properties.Any(ap => ap.ArticleCode == a.ArticleCode && ap.var4 == var4 && ap.var5 == var5);
var p_a = detailExpr.Parameters[0];
var p_var4 = detailExpr.Parameters[1];
var p_var5 = detailExpr.Parameters[2];
var body = dataFilter
.Select(filter => filter.Value
.Select(filterDetail => detailExpr.Body
.ReplaceParameter(p_var4, Expression.Constant(filter.Key))
.ReplaceParameter(p_var5, Expression.Constant(filterDetail.Key)))
.Aggregate(Expression.OrElse))
.Aggregate(Expression.AndAlso);
var predicate = Expression.Lambda<Func<Article, bool>>(body, p_a);
然后使用Where(predicate)
代替当前的Where(query)
。
答案 1 :(得分:0)
直接使用查询而不是使用字符串:
IQueryable<ArticleSummary> Articles = _DbContext.Article
.Where(a => a.var1 == SomeLocalVariable1)
.Where(a => a.var2 == SomeLocalVariable2 || a.var2 == SomeLocalVariable3)
.Where(query);
foreach(...) {
Articles = Articles.Where(...);
}
Articles = Articles.OrderByDescending(a => a.var6)
.Select(a => new ArticleSummary
{
ArticleCode = a.ArticleCode ,
var7 = a.var1
var8 = a.var3
});