Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Specific Range of List Items (LINQ)

Tags:

c#

linq

I have this block of code I'm working with :

// get the collection of librarys from the injected repository
librarySearchResults = _librarySearchRepository.GetLibraries(searchTerm);

// map the collection into a collection of LibrarySearchResultsViewModel view models
libraryModel.LibrarySearchResults =
    librarySearchResults.Select(
        library =>
        new LibrarySearchResultsViewModel
        {
            Name = library.Name,
            Consortium = library.Consortium,
            Distance = library.Distance,
            NavigateUrl = _librarySearchRepository.GetUrlFromBranchId(library.BranchID),
            BranchID = library.BranchID
        }).ToList();

All this does is take the results of GetLibraries(searchTerm), which returns a list of LibrarySearchResult objects, and maps them over to a list of LibrarySearchResultsViewModel's.

While this works well for small result sets, once I get up into the 1,000's, it really starts to drag, taking about 12 seconds before it finishes the conversion.

My question :

Since I'm using paging here, I really only need to display a fraction of the data that's being returned in a large result set. Is there a way to utilize something like Take() or GetRange(), so that the conversion only happens for the records I need to display? Say out of 1,000 records, I only want to get records 20 through 40, and convert them over to the view models.

I'm all for any suggestions on improving or refactoring this code as well.

like image 418
X3074861X Avatar asked Jul 25 '13 16:07

X3074861X


2 Answers

Use Skip and Take:

// take records from 20 to 40
var records = librarySearchResults.Skip(20).Take(20);

You can easily paginate it (you'll need page and pageSize).

On the other hand you're using ToList there, consider using just IEnumerable, conversion to list can eat up lots of time, especially for large data set.

like image 181
Zbigniew Avatar answered Nov 07 '22 02:11

Zbigniew


you can use Skip() and Take() together to enable paging.

var idx = // set this based on which page you're currently generating
librarySearchResults.Skip(idx * numitems).Take(numitems).Select(lib => ...);
like image 20
Mike Corcoran Avatar answered Nov 07 '22 00:11

Mike Corcoran