加入或Mutible DBContext存储库通用c#

我有存储库通用,我做的方法是Get,Update,Insert。

我从数据库中的表中获取数据我使用此方法。

public IEnumerable Get(Expression<Func> newObjectEntity,int page, int rowsByPage) where typeEntity : class { List Result = null; Result = Context.Set().Where(newObjectEntity).OrderBy(m => true).Skip(5 * (page - 1)).Take(rowsByPage).ToList(); return Result; } 

我在获取数据时只有一个表这是我的代码:

 var collecProducts = repository.Get(c => true); 

我的问题是,当我想要两个平板电脑我怎么做? 我发现这段代码但速度很慢。

 var collecProducts = repository.Get(c => true); var collecCategory = repository.Get(c => true); var collectProductToCategory = (from p in collecProducts join c in collecCategory on p.idCategory equals c.idCategory).ToList(); 

这段代码的问题在于获取所有数据产品和类别,并且我希望从SQL Server只需要数据,例如作为连接TSQL。

 select p.idProducts from products p join category c on p.idCategory = c.idCategory 

总结如何从仓库generyc获取数据使用加入。

谢谢,

我找到一个表单获取examenple产品和类别在存储库Generic中的结果通过include。

  public IEnumerable Get(Expression> newObjectEntity,int page, int rowsByPage, string include) where typeEntity : class { List Result = null; Result = Context.Set().Where(newObjectEntity).include(include).OrderBy(m => true).Skip(5 * (page - 1)).Take(rowsByPage).ToList(); return Result; } 

使用include,您可以将例如Product-> Category作为字符串并获取此对象。

谢谢

您找到的解决方案很慢,因为存储库方法立即实现/执行查询,而不是允许延迟执行。 尝试从存储库方法中的查询中删除“.ToList()”:

 public IEnumerable Get(Expression> newObjectEntity,int page, int rowsByPage) where typeEntity : class { IEnumerable Result = null; Result = Context.Set().Where(newObjectEntity).OrderBy(m => true).Skip(5 * (page - 1)).Take(rowsByPage); return Result; } 

这将允许您编写和构造更高阶的查询,而无需立即将整个表拉入内存。