C
C#9mo ago
nox7

[EF Core/LINQ] Paginated Eager-Loaded One-to-Many

I can't seem to find (maybe my Googling is bad) the proper way - if it's possible - to limit the returns of an eager-loaded one-to-many collection.
var form = DbContext.Forms
.Find(1)
.Include(form => form.Submission);
var form = DbContext.Forms
.Find(1)
.Include(form => form.Submission);
In my case a form could have millions of them - is it possible to limit the data fetched via .Include()? Similar to how Take() and Skip() work on the main query?
2 Replies
Angius
Angius9mo ago
You should not be using .Include() and this is one of the reasons .Select() gives you much more control
var form = await _context.Forms
.Where(f => f.Id == id)
.Select(f => new FormDto {
Id = f.Id,
Title = f.Title,
// other props...
Submissions = f.Submissions
.Skip(skip)
.Take(take)
.Select(s => new SubmissionDto {
// ...
})
.ToList(),
// other props...
})
.FirstOrDefaultAsync();
var form = await _context.Forms
.Where(f => f.Id == id)
.Select(f => new FormDto {
Id = f.Id,
Title = f.Title,
// other props...
Submissions = f.Submissions
.Skip(skip)
.Take(take)
.Select(s => new SubmissionDto {
// ...
})
.ToList(),
// other props...
})
.FirstOrDefaultAsync();
nox7
nox7OP9mo ago
Sorry for the late reply - but I see now. Thanks
Want results from more Discord servers?
Add your server