簡體   English   中英

ToList() 在轉換具有多個對象的 IEnumerable 時需要很長時間

[英]ToList() taking to long when converting an IEnumerable with multiple objects

我有以下課程:

public class CountryVM
    {
        #region Properties
        public int CountryID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        public CountryVM()
        {

        }

        /// <summary>
        /// Constructor that creates a View Model based of an Entity object
        /// </summary>
        /// <param name="countryCode">Fills data to View Model</param>
        public CountryVM(Country_Code countryCode)
        {
            if (countryCode != null)
            {
                CountryID = countryCode.Country_Code_ID;
                Code = countryCode.Country_Code1;
                Name = countryCode.Country_Name;
            }
        }
        #endregion
    }


public class StateVM
    {
        #region Properties
        public int StateID { get; set; }
        public int CountryID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }

        public CountryVM Country { get; set; }
        #endregion

        #region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        public StateVM()
        {

        }

        /// <summary>
        /// Constructor that creates a View Model based of an Entity object
        /// </summary>
        /// <param name="stateCode">Fills data to View Model</param>
        public StateVM(State_Code stateCode)
        {
            if (stateCode != null)
            {
                StateID = stateCode.State_Code_ID;
                CountryID = stateCode.Country_Code_ID;
                Code = stateCode.State_Code1;
                Name = stateCode.State_Name;

                Country = new CountryVM(stateCode.Country_Code);
            }
        }
        #endregion
    }

Country_Code 和 State_Code 都是從我的表中轉換為實體對象的表。

我運行以下代碼行:

    IEnumerable<State_Code> entityList = _stateRepository.GetAllStateCodes();
    IEnumerable<StateVM> viewModelList = entityList.Select(s => new StateVM(s));
    viewModelList = viewModelList.ToList();

跑步時

viewModelList = viewModelList.ToList(); 

需要 1 到 3 秒。 我正在試驗它並刪除了:

Country = new CountryVM(stateCode.Country_Code);

來自 StateVM 對象,它將完美運行。

所以,我猜它花費太長時間的原因是因為一旦 StateVM 被實例化,CountryVM 將在內部實例化。

有什么方法可以提高性能嗎?

運行 ToList() 將始終枚舉集合。 您遇到的是所有延遲加載的屬性,這些屬性鏈接到執行生成的 SQL 語句以獲取附加數據屬性的其他表。 如果您不需要它們,您應該過濾您的集合,直到您確定您擁有繼續進行所需的最少數據量。 如果你知道你會需要它們,你可以使用預先加載來加載原始請求中的所有內容,而不是在對象到屬性到屬性的基礎上。 看看ObjectQuery.Include

如果您使用 Visual Studio 2013 Premium(或更高版本的等效版本),請在執行代碼時留意診斷工具調試器窗口。 跟蹤將使您很好地了解檢索實體時發生的情況。

當實體與將要導航的其他實體有關系時,一個常見的錯誤是依賴於實體框架的默認延遲加載行為。 根據你的情況,很可能是,當你行使查詢N + 1個SELECT語句執行-一個讓所有的狀態代碼,並為每個國家或地區代碼(包括State_CodeCountry_Code似乎是自己的實體)。

有關額外提示,請參閱實體框架加載 您還可以搜索“Entity Framework N+1”,因為這是性能問題的常見原因。

有什么方法可以提高性能嗎?

提高性能的最佳方法是創建一個僅包含您實際需要的列(包括任何聯接)的視圖。 這將使數據庫更有效地工作,並為您提供使用覆蓋索引以獲得更好性能的選項。

第二個最佳選擇是使用 .Include,如 Jonathon Chase 的回答中所述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM