I need to use where in ThenInclude
var templatesFields = await _context.Sections
.Include(x => x.Subtitles)
.ThenInclude(r => r.Fields.Where(t=>t.TemplatesFields.TemplateID==TemplateID))
.ThenInclude(r => r.OptionSources)
.ThenInclude(r => r.OptionsSourcesDetails)
.ToListAsync();
Entity Framework Classic ThenIncludeThe ThenInclude method moves the chaining level to the property included. It allows us to include related objects from the next level. ThenInclude is a syntactic sugar method to make it easier and clearer to include multiple related objects.
Entity Framework Core supports eager loading of related entities, same as EF 6, using the Include() extension method and projection query. In addition to this, it also provides the ThenInclude() extension method to load multiple levels of related entities. (EF 6 does not support the ThenInclude() method.)
Entity Framework Core allows you to use the navigation properties in your model to load related entities. There are three common O/RM patterns used to load related data. Eager loading means that the related data is loaded from the database as part of the initial query.
you cannot use where condition inside Include
or ThenInclude
. What you can do is:
var templatesFields = await _context.Sections
.Include(x => x.Subtitles)
.ThenInclude(r => r.Fields)
.ThenInclude(r => r.OptionSources)
.ThenInclude(r => r.OptionsSourcesDetails)
.Where(t=>t.Subtitles.Fields.Any(x => x.TemplatesFields.TemplateID==TemplateID))
.ToListAsync();
EDIT: This will be possible with .Net Core 5.0 (which is in preview right now)
var blogs = context.Blogs
.Include(e => e.Posts.Where(p => p.Title.Contains("Cheese")))
.ToList();
Filtered Include
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With