我通過自己的ID獲取單個實體寫入方法檢索實體泛型方法:實體框架4 - 通過ID
public Customer GetCustomer(int i_CustomerID)
{
return (from c in context.CustomerSet
where c.Id == i_CustomerID
select c).SingleOrDefault();
}
public Movie GetMovie(int i_MovieID)
{
return (from m in context.MovieSet
where m.Id == i_MovieID
select m).SingleOrDefault();
}
但我有許多實體和重演這段代碼。我想寫一個這樣的方法:
public T GetEntityByID<T>(int i_EntityID)
{
return (from e in context.T_Set
where e.Id == i_EntityID
select e).SingleOrDefault();
}
有沒有辦法做到這一點?
你可以找到一個類似的問題[這裏](http://stackoverflow.com/questions/248682/create-llblgen-linq-query-dynamicly-with-strings) – Menahem 2011-03-27 09:58:30