FilipRot
FilipRot

Reputation: 83

Mapping a large List of Entities to a PagedList of ViewModels

How to use PagedList in a web application based on ASP MVC in combination with ViewModels.

I want to make a PagedList of Viewmodels, so first I have to map the Entities to Viewmodels.

When I fetch a large list, it it very slow because first the mapping is executed before the PagedList is made (so the whole List of Entities is fetched).

What is the best way to solve this issue?

IEnumerable<InvoiceData> invoiceData = dataService.GetInvoiceData(locationId);

Mapper.CreateMap<InvoiceData, ViewModelInvoiceData>();
IEnumerable<ViewModelInvoiceData> vmInvoiceData = Mapper.Map<IEnumerable<InvoiceData>, IEnumerable<ViewModelInvoiceData>>(invoiceData);

IPagedList<InvoiceDataViewModel> pagedListVMInvoiceData = vmInvoiceData.ToPagedList(page, pageSize);

(I also use a mapping framework, but it's of course the same problem with a custom mapper class)

Upvotes: 5

Views: 1670

Answers (2)

Camille S&#233;vigny
Camille S&#233;vigny

Reputation: 5143

I had the same issue. I sped up my results (they are still not that fast, but faster) by using the IQueryable interface instead of the IEnumerable

Change this:

IEnumerable<InvoiceData> invoiceData = dataService.GetInvoiceData(locationId);

To this:

IQueryable<InvoiceData> invoiceData = dataService.GetInvoiceData(locationId);

Upvotes: 0

Dmytro Rudenko
Dmytro Rudenko

Reputation: 2524

Why you didn't page your data on server? And after that create manual pager like in example 2 from troygoode / PagedList ?

Upvotes: 1

Related Questions