Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing System.Data.Linq.Table<T> into Generic Function

Tags:

c#

generics

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 reference 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.

like image 597
Jason Avatar asked Feb 14 '11 03:02

Jason


1 Answers

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.

like image 175
The Scrum Meister Avatar answered Oct 02 '22 15:10

The Scrum Meister