Ankur Arora
Ankur Arora

Reputation: 390

Linq Skip and Take function makes the navigation properties null in EF core

I have an IQueryable object which contains a huge number of records. I want to apply pagination on this by using LINQ Skip and Take functions. This is the kind of structure I have and I am using navigation properties using EF core.

{
Permission: {
   User: {
    Roles: List<Role>
   }
  }
}

LINQ query

var permissionQuery = from user in _context.users
                    .Include(x => x.Roles)
                        .ThenInclude(x => x.BusinessRole)
                    join permission in _context.Permissions
                    .Include(x => x.PermissionOperations)
                    on user.Id equals permission.UserId
                    select new Entities.Permission
                    {
                        PermissionOperations = permission.PermissionOperations,
                        User = user,
                    };

Pagination

permissionQuery = permissionQuery.Skip(0).Take(10)
return permissionQuery.ToList() // This makes Roles property null

My LINQ query is well constructed and it returns me the result as expected but when I apply pagination, my roles property is getting null which is quite surprising.

Pagination should only filter the records, it should not make the internal properties null. Could anyone please throw some light on this

Upvotes: 0

Views: 442

Answers (0)

Related Questions