0
我已經開始思考通用EF做法,我發現後http://blog.damianbrady.com.au/2012/03/07/a-generic-crud-repository-for-entity-framework/通用實體框架和IQueryable的<T>
的代碼片段:
protected Dictionary<string, object> CachedObjects = new Dictionary<string, object>();
protected ObjectSet<TEntity> GetObjectSet<TEntity>() where TEntity : EntityObject
{
var fulltypename = typeof(TEntity).AssemblyQualifiedName;
if (fulltypename == null)
throw new ArgumentException("Invalid Type passed to GetObjectSet!");
if (!CachedObjects.ContainsKey(fulltypename))
{
var objectset = base.CreateObjectSet<TEntity>();
CachedObjects.Add(fulltypename, objectset);
}
return CachedObjects[fulltypename] as ObjectSet<TEntity>;
}
...
public TEntity GetFirstOrDefault<TEntity>(Func<TEntity, bool> predicate) where TEntity : EntityObject
{
return GetObjectSet<TEntity>().FirstOrDefault(predicate);
}
現在,如果這兩條線的代碼是平等的?
adapter.db.GetFirstOrDefault<MyTable>(x => x.ID == 1);
adapter.db.MyTable.FirstOrDefault(x => x.ID == 1);
我在問,因爲我不想將所有項目加載到內存中,然後對它們進行一些過濾。有沒有辦法像IQueryable<T>
那樣進行這種通用操作?
什麼是'adapter.db'?什麼是'adapter.db.MyTable'? – 2012-08-10 13:12:57