Use Include() method in repository

后端 未结 3 965
眼角桃花
眼角桃花 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条回答
  •  没有蜡笔的小新
    2021-01-02 06:58

    Try:

    Change

    Expression> criteria
    

    To

    Expression> criteria
    

    Edit: To Include multiple entities, you need to add an "include" extension:

    public static class IncludeExtension
    {
        public static IQueryable Include(this IDbSet dbSet,
                                                params Expression>[] includes)
                                                where TEntity : class
        {
            IQueryable query = null;
            foreach (var include in includes)
            {
                query = dbSet.Include(include);
            }
    
            return query == null ? dbSet : query;
        }
    }
    

    Then you can use it like this:

    repository.Include(x => x.Pack, x => x.Pack.Roles, ...).Select(x => x.Pack.Id).ToList();
    

    Just make sure "repository" return a "DbSet" Object.

提交回复
热议问题