Entity Framework 5. Multiple Include. Is this possible?

后端 未结 3 755
忘掉有多难
忘掉有多难 2020-12-20 20:50

I am trying to create a multiple include method in my repository to use as follows:

repository.Include(x => x.Images, x => x.Tags).First(x          


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 21:33

    I guess the shortest way is as follows:

    public static class LinqExtensions
    {
        /// 
        /// Acts similar of .Include() LINQ method, but allows to include several object properties at once.
        /// 
        public static IQueryable IncludeMultiple(this IQueryable query, params Expression>[] paths)
            where T : class
        {
            foreach (var path in paths)
                query = query.Include(path);
    
            return query;
        }
    }
    

提交回复
热议问题