Passing System.Data.Linq.Table<T> into Generic Function
I'm trying to make a generic function that takes in a System.Data.Linq.Table<T>
.
Generic Function
public int Get<T>(MyDataContext db, System.Data.Linq.Table<T> table, string PropertyValue) where T: IMatchable
{
T prop = table.FirstOrDefault<T>(p => p.Name.ToLower() == PropertyValue.ToLower());
if (prop != null)
{
return prop.ID;
}
}
Interface so that I can access ID Property
public interface IMatchable
{
int ID { get; set; }
}
My Error
The type 'T' must be a refer开发者_开发技巧ence type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Linq.Table'
I'm not sure what I am doing wrong.
Try specifying where T: class, IMatchable
Since the System.Data.Linq.Table
has a constraint where TEntity : class
You need to make sure that T
is also a reference type.
精彩评论