Use Include() method in repository

后端 未结 3 952
眼角桃花
眼角桃花 2021-01-02 06:04

I have the following with EF 5:

var a = context.Posts.Include(x => x.Pack).Select(x => x.Pack.Id).ToList();

This works. Then I tried

3条回答
  •  旧时难觅i
    2021-01-02 06:53

    I used the accepted answer but had to modify it a bit for EntityFramework Core. In my experience I had to keep the chain going or the previous query references were overwritten.

        public IQueryable Include(params Expression>[] includes)
        {
            IIncludableQueryable query = null;
    
            if(includes.Length > 0)
            {
                query = _dbSet.Include(includes[0]);
            }
            for (int queryIndex = 1; queryIndex < includes.Length; ++queryIndex)
            {
                query = query.Include(includes[queryIndex]);
            }
    
            return query == null ? _dbSet : (IQueryable)query;
        }
    

提交回复
热议问题