Multiple Includes() in EF Core

后端 未结 8 2165
傲寒
傲寒 2020-12-08 08:07

I have an extension method that lets you generically include data in EF:

public static IQueryable IncludeMultiple(this IQueryable          


        
8条回答
  •  鱼传尺愫
    2020-12-08 08:45

    I adhere the simpler solution that makes use of the Include() overload that uses string navigationPropertyPath. The simplest that I can write is this extension method below.

    using Microsoft.EntityFrameworkCore;
    using System.Linq;
    
    namespace MGame.Data.Helpers
    {
        public static class IncludeBuilder
        {
            public static IQueryable Include(this IQueryable queryable, params string[] navigations) where TSource : class
            {
                if (navigations == null || navigations.Length == 0) return queryable;
    
                return navigations.Aggregate(queryable, EntityFrameworkQueryableExtensions.Include);  // EntityFrameworkQueryableExtensions.Include method requires the constraint where TSource : class
            }
        }
    }
    

提交回复
热议问题