我在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
我试图找到一种方法将包含语句的集合传递到我的存储库,以便我可以让它包含特定的实体.以下是我的存储库中的一些示例代码.
public TEntity GetById(Guid id)
{
return id != Guid.Empty ? GetSet().Find(id) : null;
}
private IDbSet<TEntity> GetSet()
{
return _unitOfWork.CreateSet<TEntity>();
}
Run Code Online (Sandbox Code Playgroud)
GetByID方法调用GetSet以返回实体集.我在想,如果我能以某种方式传入一组实体来包含(通过表达式)作为我的GetById的一部分,这样我就不必将GetSet公开给我的服务了.所以,像这样:
var entity = _repository.GetById(theId,e => {e.Prop1,e.Prop2,e.Prop3});
然后我可以将该表达式传递给我的GetSet方法并将其传递给include语句.思考?
c# ×1