我在Entity Framework Code First之上使用通用存儲庫模式。一切工作正常,直到我需要在查詢中包含更多實(shí)體。我必須成功包含一個(gè)實(shí)體,但是現(xiàn)在我不知道如何包含多個(gè)實(shí)體。看看到目前為止我得到了什么:public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class{ var entityName = GetEntityName<TEntity>(); return _objectContext.CreateQuery<TEntity>(entityName);}public IList<TEntity> GetQueryWithInclude<TEntity>(string toInclude) where TEntity : class{ var entityName = GetEntityName<TEntity>(); return _objectContext.CreateQuery<TEntity>(entityName).Include(toInclude).ToList();}private string GetEntityName<TEntity>() where TEntity : class{ return string.Format("{0}.{1}", _objectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));}我試圖做但沒用的是將字符串?dāng)?shù)組傳遞給函數(shù),然后嘗試將“包含”附加到查詢頂部。我想知道如果我一次調(diào)用GetQueryWithInclude并一次傳遞一個(gè)實(shí)體名稱(實(shí)際上是一個(gè)導(dǎo)航屬性)來聚合查詢結(jié)果該怎么辦,但是我擔(dān)心這可能會(huì)在每次調(diào)用時(shí)重復(fù)查詢的結(jié)果...您認(rèn)為什么是使它正常工作的最佳方法?提前致謝!更新:這是我要實(shí)現(xiàn)的目標(biāo)的一個(gè)示例:public IQueryable GetQueryWithIncludes(string[] otherEntities){ var entityName = GetEntityName<TEntity>(); //now loop over the otherEntities array //and append Include extensions to the query //so inside the loop, something like: _objectContext.GetQuery<TEntity>(entityName).Include(otherEntities[index]);}
3 回答

當(dāng)年話下
TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超9個(gè)贊
與硬編碼的ObjectQuery(T)告別。
如果您使用的是EF> 4,那么它是內(nèi)置的,請檢查MSDN上的DbExtensions.Include。
- 3 回答
- 0 關(guān)注
- 482 瀏覽
添加回答
舉報(bào)
0/150
提交
取消