将 System.Data.Linq.Table<T> 传递到泛型函数中

6
我将尝试创建一个通用函数,它接受一个 System.Data.Linq.Table<T>

通用函数

 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;
     }
 }

接口使我可以访问ID属性

public interface IMatchable
{
    int ID { get; set; }
}

我的错误

类型 'T' 必须是引用类型,才能将其用作泛型类型或方法 'System.Data.Linq.Table' 中的参数 'TEntity'

我不确定我的错误在哪里。

1个回答

6

尝试指定 where T: class, IMatchable

由于System.Data.Linq.Table有一个约束条件where TEntity : class,因此您需要确保T也是引用类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接