gpt4 book ai didi

c# - 将 System.Data.Linq.Table 传递给通用函数

转载 作者:太空狗 更新时间:2023-10-29 21:36:28 26 4
gpt4 key购买 nike

我正在尝试制作一个接受 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;
}
}

接口(interface),以便我可以访问 ID 属性

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

我的错误

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'

我不确定我做错了什么。

最佳答案

尝试指定 where T: class, IMatchable

System.Data.Linq.Table有一个约束 where TEntity : class 你需要确保 T 也是一个引用类型。

关于c# - 将 System.Data.Linq.Table<T> 传递给通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com