开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜