我写了下面的方法。
public T GetByID(int id)
{
var dbcontext = DB;
var table = dbcontext.GetTable<T>();
return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) == id);
}
基本上它是通用类中的一个方法,其中T
是 DataContext 中的一个类。
该方法从 T 的类型获取表(GetTable
)并检查输入参数的第一个属性(始终是 ID)。
问题是我必须首先将元素表转换为列表才能执行GetType
属性上,但这不是很方便,因为必须枚举表的所有元素并将其转换为List
.
我如何重构这个方法以避免ToList
整个桌子上?
[Update]
我无法执行的原因Where
直接在桌子上是因为我收到这个异常:
方法“System.Reflection.PropertyInfo[] GetProperties()”不支持对 SQL 的转换。
Because GetProperties
无法翻译成SQL。
[Update]
有些人建议使用接口T,但问题是T
参数将是自动生成的类[DataContextName].designer.cs,因此我无法让它实现一个接口(并且实现 LINQ 的所有这些“数据库类”的接口是不可行的;而且,一旦我向 DataContext 添加新表,该文件就会重新生成,从而丢失所有写入的内容数据)。
因此,必须有更好的方法来做到这一点......
[Update]
我现在已经实现了我的代码尼尔·威廉姆斯 https://stackoverflow.com/questions/735140/c-linq-to-sql-refectoring-this-generic-getbyid-method/735209#735209'建议,但我仍然遇到问题。以下是代码摘录:
界面:
public interface IHasID
{
int ID { get; set; }
}
数据上下文[查看代码]:
namespace MusicRepo_DataContext
{
partial class Artist : IHasID
{
public int ID
{
get { return ArtistID; }
set { throw new System.NotImplementedException(); }
}
}
}
通用方法:
public class DBAccess<T> where T : class, IHasID,new()
{
public T GetByID(int id)
{
var dbcontext = DB;
var table = dbcontext.GetTable<T>();
return table.SingleOrDefault(e => e.ID.Equals(id));
}
}
该行抛出异常:return table.SingleOrDefault(e => e.ID.Equals(id));
例外是:
System.NotSupportedException: The
member
'MusicRepo_DataContext.IHasID.ID' has
no supported translation to SQL.
[更新]解决方案:
在...的帮助下丹尼斯·特罗尔 https://stackoverflow.com/questions/735140/c-linq-to-sql-refectoring-this-generic-getbyid-method/735888#735888的发布答案以及该帖子的链接代码咆哮博客 http://mikehadlow.blogspot.com/2008/03/using-irepository-pattern-with-linq-to.html,我终于找到了解决方案:
public static PropertyInfo GetPrimaryKey(this Type entityType)
{
foreach (PropertyInfo property in entityType.GetProperties())
{
ColumnAttribute[] attributes = (ColumnAttribute[])property.GetCustomAttributes(typeof(ColumnAttribute), true);
if (attributes.Length == 1)
{
ColumnAttribute columnAttribute = attributes[0];
if (columnAttribute.IsPrimaryKey)
{
if (property.PropertyType != typeof(int))
{
throw new ApplicationException(string.Format("Primary key, '{0}', of type '{1}' is not int",
property.Name, entityType));
}
return property;
}
}
}
throw new ApplicationException(string.Format("No primary key defined for type {0}", entityType.Name));
}
public T GetByID(int id)
{
var dbcontext = DB;
var itemParameter = Expression.Parameter(typeof (T), "item");
var whereExpression = Expression.Lambda<Func<T, bool>>
(
Expression.Equal(
Expression.Property(
itemParameter,
typeof (T).GetPrimaryKey().Name
),
Expression.Constant(id)
),
new[] {itemParameter}
);
return dbcontext.GetTable<T>().Where(whereExpression).Single();
}