在我以后的EF中,我试图传入一个匿名函数作为我的Linq查询的一部分.该函数将传入INT并返回BOOL(u.RelationTypeId是INT).以下是我的功能的简化版本:
public IEnumerable<UserBandRelation> GetBandRelationsByUser(Func<int, bool> relation)
{
using (var ctx = new OpenGroovesEntities())
{
Expression<Func<UsersBand, bool>> predicate = (u) => relation(u.RelationTypeId);
var relations = ctx.UsersBands.Where(predicate);
// mapping, other stuff, back to business layer
return relations.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到上述错误.看起来我通过从函数构建谓词来使一切正确.有任何想法吗?谢谢.
给定一个DoSomething采用(无参数)函数的方法并以某种方式处理它.有没有更好的方法为参数的函数创建"重载"而不是下面的代码片段?
public static TResult DoSomething<TResult>(Func<TResult> func)
{
//call func() and do something else
}
public static TResult DoSomething<T0, TResult>(
Func<T0, TResult> func,
T0 arg0)
{
return DoSomething(() => func(arg0));
}
public static TResult DoSomething<T0, T1, TResult>(
Func<T0, T1, TResult> func,
T0 arg0, T1 arg1)
{
return DoSomething(arg => func(arg, arg1), arg0);
}
public static TResult DoSomething<T0, T1, T2, TResult>(
Func<T0, T1, T2, TResult> func,
T0 arg0, T1 arg1, T2 arg2)
{
return DoSomething(arg => func(arg, arg1, arg2), arg0); …Run Code Online (Sandbox Code Playgroud) 我知道从MSDN的文章有关如何修改表达式树什么的ExpressionVisitor是应该做的.它应该修改表达式.
然而,他们的例子非常不现实,所以我想知道为什么我需要它?你能说出一些真实世界的案例吗?修改表达式树会有意义吗?或者,为什么必须进行修改?从什么到什么?
它还有许多重载用于访问各种表达式.我如何知道何时应该使用它们以及它们应该返回什么?另一方面,我看到人们使用VisitParameter并返回base.VisitParameter(node)另一方正在返回Expression.Parameter(..).