我在Entity Framework Code First上使用Generic Repository模式.一切都工作正常,直到我需要在查询中包含更多实体.我成功地包含了一个实体,但现在我无法弄清楚如何包含多个实体.看看到目前为止我得到了什么:
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));
}
Run Code Online (Sandbox Code Playgroud)
我尝试但没有工作的是将一个字符串数组传递给一个函数,然后尝试在查询之上"追加"包含.我在想,如果我叫GetQueryWithInclude并在同一时间聚集查询的结果通过了实体名称(实际导航性能),但我担心这可能会重复在每次调用查询的结果...您认为最好的方法是什么?
提前致谢!
更新:
这是我想要实现的一个例子:
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 …Run Code Online (Sandbox Code Playgroud) c# entity-framework repository-pattern entity-framework-4 ef-code-first