c# - 转换具有多个对象的 IEnumerable 时,ToList() 花费很长时间

标签 c# entity-framework linq collections

我有以下类(class):

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

关于c# - 转换具有多个对象的 IEnumerable 时,ToList() 花费很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37174936/

相关文章:

c# - WPF 如何创建具有验证和绑定(bind)的自定义文本框

c# - 如何对两个主键使用 FirstOrDefault

c# - 使用 IDispose 关闭非托管资源

sql - 使用 Entity Framework 4.1 的多表连接,我应该使用 lambda 还是 LINQ?

C# WebClient 下载文件到绝对路径

c# - Entity Framework 6.1 Code First MySql 实体跟踪在上下文中持续存在

c# - ASP.Net MVC 应用程序看似忽略数据库连接字符串

c# - 如何将 C# 8.0 可空引用类型与 Entity Framework Core 模型一起使用?

c# - FileDialog 过滤器 - LINQ 连接

c# - int ID 属于 List<int> 的 linq 查询