我有這個代碼塊我的工作:獲取列表項(LINQ)的具體範圍
// 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();
這一切確實是採取GetLibraries(searchTerm)
,返回LibrarySearchResult
對象列表的結果,將它們映射到LibrarySearchResultsViewModel
的列表中。
雖然這適用於小型結果集,但一旦達到1,000,它確實會開始拖動,大約需要12秒才能完成轉換。
我的問題:
由於我使用分頁這裏,我真的只需要顯示在一個大的結果集返回的數據的一小部分。有沒有辦法使用類似Take()
或GetRange()
的東西,以便轉換隻發生在我需要顯示的記錄上?在1,000條記錄中,我只想獲取20到40條記錄,並將它們轉換爲視圖模型。
我對這些代碼的改進或重構都有任何建議。
請參閱關於簽名的常見問題解答。 http://stackoverflow.com/help/behavior具體'不要使用簽名,標語或問候語。 您製作的每篇文章都已經用您的標準用戶卡「簽名」,該用戶卡直接鏈接回用戶頁面。如果您使用額外的簽名或標語,它將被刪除,以減少問題和答案中的噪音。' – cadrell0
很高興爲您提供幫助。我建議您閱讀完整的FAQ,以確保遵循社區準則。我也推薦這篇文章的meta瞭解更多信息。 http://meta.stackexchange.com/questions/7931/faq-for-stack-exchange-sites – cadrell0