I wanted to write this small, trivial extension method for some throwaway test / console app and I haven’t been able to figure out how to get the method signature right. I’m looking to add this to every System.Data.Linq.Table that I have and I want it to take an instance of type T as a parameter.
Here’s what I have so far that doesn’t compile
public static void InsertAndSubmit<T>(this System.Data.Linq.Table<T> tbl, T element)
{
tbl.InsertOnSubmit(element);
tbl.Context.SubmitChanges();
}
The type ‘T’ must be a reference type in order to use it as parameter
‘TEntity’ in the generic type or method
‘System.Data.Linq.Table’
Try this:
You need to constrain the type of
Tto be a reference type (class) in order for this to work.If you look at
Table<TEntity>‘s documentation you will notice this same type constraint onTEntityofclass. So for yourTto be compatible withTEntityit must meet the same constraints.