Join or Mutible DBContext Repository Generic c#

纵饮孤独 提交于 2019-11-27 16:32:23

Thank,

I find a form get this result for examenple Product and Category in repository Generic through include.

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

With include you can string for example Product->Category and get this object.

Thanks

The solution you found is slow because the repository method is materializing/executing the query immediately instead of allowing deferred execution to occur. Try removing the ".ToList()" from the query within the repository method:

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

This will allow you to compose and construct higher order queries without pulling the whole table into memory immediately.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!