Multiple Includes() in EF Core

后端 未结 8 2174
傲寒
傲寒 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:53

    You can do something like this:

    public Patient GetById(int id, Func, IIncludableQueryable> includes = null)
            {
                IQueryable queryable = context.Patients;
    
                if (includes != null)
                {
                    queryable = includes(queryable);
                }
    
                return  queryable.FirstOrDefault(x => x.PatientId == id);
            }
    
    var patient = GetById(1, includes: source => source.Include(x => x.Relationship1).ThenInclude(x => x.Relationship2));
    

提交回复
热议问题