C# 如何构建这个复杂的表达式Func?
我目前正在一个C# 如何构建这个复杂的表达式Func?,c#,entity-framework,lambda,expression,C#,Entity Framework,Lambda,Expression,我目前正在一个EF网站上工作,在那里我有这个数据库对象: public partial class Association { public Association() { this.Users = new HashSet<User>(); } public Guid Id { get; set; } public Guid ItemId { get; set; } [ForeignKey("ItemId")]
EF
网站上工作,在那里我有这个数据库对象:
public partial class Association
{
public Association()
{
this.Users = new HashSet<User>();
}
public Guid Id { get; set; }
public Guid ItemId { get; set; }
[ForeignKey("ItemId")]
public virtual Item Item { get; set; }
[InverseProperty("Association")]
public virtual ICollection<User> Users { get; set; }
}
我试着这样称呼它:
var expr = MyHelper.MyExprMethod((Association x) => x.Item.Type, (Association b) => b.Users.Count, Item.MaxOccupationRange);
但显然,它抱怨b
参数未分配,因为它没有出现在结果lambda中。我尝试了很多方法(匿名类型而不是两个源等等),但似乎我对这个概念太不熟悉了,我找不到生成这样一个表达式的方法
public static Expression<Func<TSource, bool>> MyExprMethod<TSource, TKey, TKey2, TValue>(Expression<Func<TSource, TKey>> source, Expression<Func<TSource, TKey2>> source2, IReadOnlyDictionary<TKey, TValue> dict)
{
var body = dict
.Aggregate((Expression)null, (next, dicEntry) => next == null
? Expression.Condition
(
Expression.LessThan(source2.Body, Expression.Constant(dicEntry.Value)),
Expression.Constant(true),
Expression.Constant(false)
)
: Expression.Condition
(
Expression.Equal(source.Body, Expression.Constant(dicEntry.Key)),
Expression.Condition
(
Expression.LessThan(source2.Body, Expression.Constant(dicEntry.Value)),
Expression.Constant(true),
next
),
next
)
);
return Expression.Lambda<Func<TSource, bool>>(body, source.Parameters[0]);
}
谁能给我看看吗
非常感谢
编辑
以下是通话内容:
var filter = PredicateUtils.True<Association>();
var expr = MyHelper.MyExprMethod((Association x) => x.Item.Type, (Association b) => b.Users.Count, Item.MaxOccupationRange);
filter = filter.And(expr);
var associations = entities.Associations.Where(filter);
return associations .OrderBy(a => a.Id).ToPagedList(pageNb, 2);
var filter=predicteutils.True();
var expr=MyHelper.MyExprMethod((关联x)=>x.Item.Type,(关联b)=>b.Users.Count,Item.MaxOccupationRange);
过滤器=过滤器和(expr);
var associations=实体.associations.Where(过滤器);
returnassociations.OrderBy(a=>a.Id).ToPagedList(pageNb,2);
这是最后一行(ToPagedList)显示的消息:参数“b”未绑定在指定的LINQ到实体查询表达式中
我想您需要使用第二个表达式
public static Expression<Func<TSource, bool>> MyExprMethod<TSource, TKey, TKey2, TValue>(Expression<Func<TSource, TKey>> source, Expression<Func<TSource, TKey2>> source2, IReadOnlyDictionary<TKey, TValue> dict)
{
source2 = PredicateRewriter.Rewrite(source2, source.Parameters[0]);
var body = dict
它在哪里抱怨b?除了
EF
之外,上面的代码还编译并运行。谢谢你的回答,我编辑了第一篇文章,希望这篇文章对你有所帮助。你希望Association x
和Association b
是同一个参数:看看我的答案,我在这里展示了如何做到这一点。这非常好,谢谢你,你让我开心^^
var filter = PredicateUtils.True<Association>();
var expr = MyHelper.MyExprMethod((Association x) => x.Item.Type, (Association b) => b.Users.Count, Item.MaxOccupationRange);
filter = filter.And(expr);
var associations = entities.Associations.Where(filter);
return associations .OrderBy(a => a.Id).ToPagedList(pageNb, 2);
public static Expression<Func<TSource, bool>> MyExprMethod<TSource, TKey, TKey2, TValue>(Expression<Func<TSource, TKey>> source, Expression<Func<TSource, TKey2>> source2, IReadOnlyDictionary<TKey, TValue> dict)
{
source2 = PredicateRewriter.Rewrite(source2, source.Parameters[0]);
var body = dict
public class PredicateRewriter
{
public static Expression<Func<T, U>> Rewrite<T,U>(Expression<Func<T, U>> exp, //string newParamName
ParameterExpression param)
{
//var param = Expression.Parameter(exp.Parameters[0].Type, newParamName);
var newExpression = new PredicateRewriterVisitor(param).Visit(exp);
return (Expression<Func<T, U>>)newExpression;
}
private class PredicateRewriterVisitor : ExpressionVisitor
{
private readonly ParameterExpression _parameterExpression;
public PredicateRewriterVisitor(ParameterExpression parameterExpression)
{
_parameterExpression = parameterExpression;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return _parameterExpression;
}
}
}