C# ToList()在转换具有多个对象的IEnumerable时需要较长时间

C# ToList()在转换具有多个对象的IEnumerable时需要较长时间,c#,entity-framework,linq,collections,C#,Entity Framework,Linq,Collections,我有以下课程: public class CountryVM { #region Properties public int CountryID { get; set; } public string Code { get; set; } public string Name { get; set; } #endregion #region Constructors ///

我有以下课程:

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
    }
这需要1到3秒钟。我正在试验它,我删除了:

Country = new CountryVM(stateCode.Country_Code);
从StateVM对象,它将完美地运行

所以,我想它花费太长时间的原因是因为一旦StateVM被实例化,CountryVM就会在里面被实例化


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

运行ToList()将始终枚举集合。您遇到的是所有延迟加载的属性,这些属性链接到执行生成的SQL语句的其他表,以获取附加的数据属性。如果您不需要它们,您应该过滤您的收集,直到您确定您拥有前进所需的最小数据量。如果您知道您将需要它们,那么可以使用“急切加载”来代替加载原始请求中的所有内容,而不是基于对象到对象到属性到属性的方式。查看一下

如果您使用Visual Studio 2013 Premium(或更高版本的等效软件),请在代码执行时密切关注诊断工具调试器窗口。当您检索实体时,跟踪会让您很好地了解发生了什么

一个常见的错误是,当实体与将被导航的其他实体有关系时,依赖实体框架的默认延迟加载行为。在您的情况下,在执行查询时可能会执行N+1个SELECT语句——一个用于获取所有州代码,另一个用于获取每个国家代码(国家代码和国家代码都是它们自己的实体)

如需更多提示,请参阅。您还可以搜索“实体框架N+1”,因为这是性能问题的常见原因

有没有办法提高绩效

提高性能的最佳方法是创建一个只包含实际需要的列(包括任何联接)的视图。这将允许数据库更高效地工作,并允许您选择使用覆盖索引以获得更好的性能

第二个最好的选择是使用。包括,正如乔纳森·蔡斯在回答中所解释的

viewModelList = viewModelList.ToList(); 
Country = new CountryVM(stateCode.Country_Code);