General2001
General2001

Reputation: 331

Skip, Take to Include

I have a model "MainCategory". How can I take 3 categories only from my mainCategory?

var mainCategory = _context.MainCategories
  .Include(c => c.Categories)
  .Skip(0)
  .Take(3)
  .FirstOrDefault();

Upvotes: 2

Views: 83

Answers (1)

ilkerkaran
ilkerkaran

Reputation: 4354

EfCore does not support that out of the box. However, the below can be used as a workaround in some specific cases

var mainCategory = _context.MainCategories
  .Select(c => new
    {
        c,
        Categories= c.Categories.OrderBy(x => x.CategoryID)
  .Skip((PageSize * PageNumber) - PageSize)
  .Take(PageSize)
    })
  .FirstOrDefault();

P.S: DbContext can't track the object anymore. This is the downside of this approach

Upvotes: 1

Related Questions