【问题标题】:ToList() taking too long to executeToList() 执行时间过长
【发布时间】:2019-09-17 23:30:36
【问题描述】:

我是 asp.net 的初学者,我正在使用 sitefinity 中的 asp.net 网站。页面上有一个自定义小部件,因此页面加载时间降至 2 分钟。

经过大量研究,我发现 ToList() 可能是这种缓慢的原因。下面是包含 ToList() 的小部件的 C# 代码。

namespace SitefinityWebApp.CustomControls
{
    public partial class DashboardRole : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<bl.CustomItem> classificationList = bl.module.GetClassifications(bl.config.classification_assets_name, "", bl.config.data_asset_content_type);
            classificationList = classificationList.OrderByDescending(o => o.ItemsCount).ToList();
            rptMainClassifications.DataSource = classificationList;
            rptMainClassifications.DataBind();
        }
    }
}

有没有人之前遇到过这样的问题。任何帮助都意义重大。

编辑:这就是 GetClassifications() 的样子 https://i.stack.imgur.com/OsnrW.jpg

【问题讨论】:

  • 代价高昂的操作是OrderByDescending(),而不是ToList(),即使这样也不会花费太长时间,除非您加载的数据比网页上显示的数据多得多。在设置DataSource 之前,您不必调用ToList()。您会发现性能没有差异
  • 另外,你确定延迟不是由 first 行引起的吗? GetClassifications 是做什么的,classificationList 中有多少项目?如果GetClassifications 查询数据库,为什么不让数据库在返回结果之前对结果进行排序?
  • ItemsCount 可能隐藏了复杂性
  • 确实,如果ItemsCount 为每个项目执行SELECT COUNT(*),您可以以101 个查询结束100 个结果项目。使用 SQL Server Profiler 或扩展事件查看执行了哪些查询。
  • 抱歉这个愚蠢的问题,但我从来没有在任何cs文件中找到这个小部件的SQL查询。你知道查询写在哪里吗?

标签: c# asp.net sitefinity


【解决方案1】:

我很确定这不是因为 ToList() 方法。
ToList() 只需将您的 IEnumerable() 或其他列表转换为简单的 c# List() .
还有一个问题。而且您的问题内容不足以理解问题。

【讨论】:

  • 感谢您的帮助。您认为还有什么原因?我应该发布相应 ascx 文件的屏幕截图吗?
猜你喜欢
  • 2018-01-19
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多