[英]Entity framework: Generic get with Icollection
我正在使用可以返回一組特定實體的通用方法。
public virtual IList<T> GetList(Func<T, bool> where,
params Expression<Func<T, object>>[] navigationProperties)
{
List<T> list;
using (var context = new eTRdataEntities())
{
IQueryable<T> dbQuery = context.Set<T>();
//Apply eager loading
foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
dbQuery = dbQuery.Include<T, object>(navigationProperty);
list = dbQuery
.AsNoTracking()
.Where(where)
.ToList<T>();
}
return list;
}
我有三個類,分別稱為Domain,Term和Word。 該域包含多個術語,一個術語可以包含多個Word實例。 因此,它們被定義為Icollections
例如
public virtual ICollection<Terms> Terms{ get; set; }
我已經使用了以下命令:
IGenericDataService<Domain> domain_= new DataManagementService<Domain>();
IList<Domain> tempDom = domain_.GetList(o => o.OID.Equals("ABC"), d => d.Term);
這將返回域及其所有術語,但是不會返回單詞。
我需要知道的是如何調用此方法並將其定向以獲取Term和Word(渴望加載)。 問題出現了,因為該域有一系列條款,我不能這樣稱呼它,
IList<Domain> tempDom = domain_.GetList(o => o.OID.Equals("ABC"), d => d.Term.Word);
請指教,
非常感謝
Include
嵌套集合:
IList<Domain> tempDom = domain_.GetList(o => o.OID.Equals("ABC"),
d => d.Term.Select(t => Words));
另請參閱: 加載相關實體 。
順便說一下,我將Func where
參數更改為Expression<Func>>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.