无法将表达式类型'lambda expression'转换为返回类型'System.Linq.Expressions.Expression <System.Func <IProduct,string,bool >>'

Rob*_*eal 4 c# lambda

好的,我迷路了.为什么第一个函数为WRONG(在lambda表达式中为squiglies),但第二个函数是RIGHT(意味着它编译)?

    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");

    }
Run Code Online (Sandbox Code Playgroud)

wom*_*omp 6

你的第一个函数需要两个参数. Func<x,y,z>定义了两个参数和返回值.既然你有一个IProduct和一个stringas参数,你的lambda中需要两个参数.

  public static Expression<Func<IProduct, string, bool>> IsValidExpression(string val)
  {
        return ((h, i) => h.product_name == val);
  }
Run Code Online (Sandbox Code Playgroud)

你的第二个函数只是Func<x,y>,这意味着函数签名只有一个参数,因此你的lambda语句编译.