I\'m trying to create a generic extension method, that works on typed data tables :
public static class Extensions
{
public static TableType DoSomething&
Eric's answer is great for explaining why the types cannot be inferred. Here are a couple of suggestions to hopefully cut down on the verbosity of the code that you will have to write.
If you can explicitly define the type of your lambda expression, then it can infer the types.
One example of how to do that is below. I've created a criteria parameter that is explicitly of type Expression. In this example, this doesn't save you much typing, but perhaps in practice you can make use of this.
MyTypedDataSet.MyTypedDataTable table = new MyTypedDataSet.MyTypedDataTable();
Expression> criteria = row => row.Field1 == "foo";
return table.DoSomething(criteria);
EDIT: altered my example to use another extension method rather than deriving a custom TypedTableBase class from System.Data.TypedTableBase.
Below is another example that can do a better job of inferring the type parameters. You define another extension method (mine is called RowPredicate) that only has one type parameter to infer. The first parameter is of type TypedTableBase, so the compiler should have no problem inferring the type from that:
public static Expression> RowPredicate(this TypedTableBase table, Expression> predicate)
where RowType : DataRow
{
return predicate;
}
This allows you to compile the following code:
MyTypedDataSet.MyTypedDataTable table = new MyTypedDataSet.MyTypedDataTable();
return table.DoSomething(table.RowPredicate(row => row.Field1 == "foo"));
Primarily the table parameter simply servers to inform the compiler of the type to use for RowType. Is this a good idea? I'm not so sure, but it does allow the compiler to infer all of the generic types.