好吧,我迷路了。为什么第一个函数是错误的(在lambda表达式中是弯曲的),而第二个函数是正确的(意味着它可以编译)?
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return (h => h.product_name == val);
}
public static Expression<Func<IProduct, bool>> IsValidExpression2()
{
return (m => m.product_name == "ACE");
}发布于 2010-01-12 06:33:10
你的第一个函数需要两个参数。定义了两个参数和返回值。因为您同时有一个IProduct和一个string作为参数,所以在您的lambda中需要两个参数。
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return ((h, i) => h.product_name == val);
}您的第二个函数仅为Func<x,y>,因此这意味着函数签名只有一个参数,因此您的lambda语句将被编译。
发布于 2010-01-12 06:32:57
中间的string打算做什么?您可以通过以下方式使其编译:
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return (h,something) => h.product_name == val;
}或者你的意思是:
public static Expression<Func<IProduct, string, bool>> IsValidExpression()
{
return (h,val) => h.product_name == val;
}发布于 2010-01-12 06:39:40
Func<IProduct, string, bool>是具有以下签名的方法的委托:
bool methodName(IProduct product, string arg2)
{
//method body
return true || false;
}所以
public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
{
return (h => h.product_name == val);
}返回类型和返回值之间存在差异。您正在尝试返回Expression<Func<IProduct, bool>>类型的对象。
val参数不是您委托给的方法的参数,但将被提升(成为实现结果函数的类的一部分),并且由于它不是结果方法的参数,因此它不应该是Func类型分离的一部分
https://stackoverflow.com/questions/2045409
复制相似问题