LINQ,类型无法推断

时间:2011-12-05 12:57:31

标签: linq

我正在学习LINQ,在执行此查询时:

public static class Linq
       {
           // Returns the given anonymous method as a lambda expression
           public static Expression<Func<Int32, Int32>>
              Expr<T, R>(Expression<Func<Int32, Int32>> f)
           {
               return f;
           }
           // Returns the given anonymous function as a Func delegate
           public static Func<T, R>
                 Func<T, R>(Func<T, R> f)
           {
               return f;
           }

   }

//main Fun

var expr = Linq.Expr((Int32 a, Int32 b) => a + b);
var fun = Linq.Func((int a, int b) => a + b);

我收到以下错误Linq.Expr<T,R>(System.Linq.Expressions.Expression<System.Func<int,int>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.错误。我做错了什么?

2 个答案:

答案 0 :(得分:2)

如果您想要两个输入参数和一个返回值,则需要使用Func<T1, T2, TRet>,即具有三个参数的一个。

答案 1 :(得分:0)

我想你想要这个:

public static class Linq { 
// Returns the given anonymous method as a lambda expression 
public static Expression<Func<T1, T2, R>> 
                    Expr<T1, T2, R>(Expression<Func<T1, T2, R>> f) 
      { 
    return f; 
} 
// Returns the given anonymous function as a Func delegate 
public static Func<T1, T2, R> Func<T1, T2, R>(Func<T1, T2, R> f) 
{ 
    return f; 
}} 

用法:

var expr = Linq.Expr<int, int, int>((a, b) => a + b);
var fun =  Linq.Func<int, int, int>((a, b) => a + b);