Luan_Nunes Luan_Nunes - 18 days ago 6
C# Question

How to paginate data from child object in EF?

I have a problem.

I want to paginate

ClassA
.

ClassA
has a list of elements of type
ClassB
, and
ClassB
has a list of element of type
ClassC
.

How can I paginate data by the number of elements from
ClassC
?


public class ClassA
{
public ICollection<ClassB> ClassBCollection { get; set; }
}

public class ClassB
{
public ICollection<ClassA> ClassACollection { get; set; }

public ICollection<ClassC> ClassCCollection { get; set; }
}

public class ClassC
{
public ICollection<ClassB> ClassBCollection { get; set; }
}


ClassA  | ClassB   | ClassC

Data1 | InnerData | InnerInnerData1

          |                   | InnerInnerData2

          |                   | InnerInnerData3

          |                   | InnerInnerData4

          |                   | InnerInnerData5 (Paginate here)


Thanks in advance.

Answer

I can resolve with System.Linq.Queryable The solution like this:

var result =  from A in DataContext<A>()
              .Include(x => x.B)
              .Include(x => x.B.C)
              .Orderby(x => x.C)
              .ThenBy(x => x.B)
              .ThenBy(x => x.A)
              .Skip(pageNumber * pageSize)
              .Take(pageSize);

That's all Folks! :)

Comments