c# - Linq 查询在几分之一秒内运行,但 .ToList() 需要 3.5 秒

标签 c# linq linq-to-entities entity-framework-5

我正在运行一个返回大约 25 条记录的 Linq 查询,每条记录有 10 个数字列。根据我的代码探查器,查询本身只需要几分之一秒——但调用 .ToList() 大约需要 3.5 秒。如前所述,从 SQL 返回的数据量很小,因此将其复制到 List 中所花费的时间应该不会很繁重。

为什么 .ToList() 需要这么长时间?如何改进?

编辑:感谢所有快速回答,让我更清楚地说明:我完全知道查询是延迟加载的。我看到的现象是 SQL Server Profiler 和 ANTS Performance Profiler 都报告说实际查询执行时间只有几分之一秒。

这是 ANTS 的屏幕截图:

enter image description here

请注意,调用方法耗时 4.3 秒,而实际 SQL 查询的耗时均不超过 0.05 秒。会不会是该方法中的其他代码,而不是 SQL?下面我们看看ANTS是如何分解这里的code profile的:

enter image description here

确凿证据:.ToList() 耗时 3.36 秒,其中可能有 0.05 秒可归因于实际查询执行时间,剩下 3.31 秒不明。

那时间去哪儿了?

编辑 2: 好吧,你要求它,所以这是我的代码:

public static Expression<Func<Student, Chart>> GetStudentAssessmentQuestionResultByStudentIdNew(MyDataEntities db)
{
    return s => new Chart
        {
            studentID = s.ID,
            Lines =
                    db.StudentAssessmentAnswers
                        .Where(
                                saa =>
                                saa.StudentAssessment.BorrowedBook.StudentID == s.ID && saa.PointsAwarded != null &&
                                saa.Question.PointValue > 0 &&
                                (saa.Question.QuestionType == QuestionType.MultipleChoice ||
                                 saa.Question.QuestionType == QuestionType.OpenEnded))
                        .GroupBy(
                                saa =>
                                new
                                {
                                    saa.StudentAssessment.AssessmentYear,
                                    saa.StudentAssessment.AssessmentMonth,
                                    saa.Question.CommonCoreStandard
                                },
                                saa => saa)
                        .Select(x => new
                                     {
                                         x.Key.AssessmentYear,
                                         x.Key.AssessmentMonth,
                                         x.Key.CommonCoreStandard,
                                         PercentagePointValue =
                                         (float)(x.Sum(a => a.PointsAwarded) * 100) / (x.Sum(a => a.Question.PointValue))
                                     })
                        .OrderByDescending(x => x.CommonCoreStandard)
                        .GroupBy(r1 => (byte)r1.CommonCoreStandard)
                        .Select(g => new ChartLine
                                     {
                                         ChartType = ((ChartType)g.Key),
                                         //type = g.Key.ToString(),
                                         type = g.Key,
                                         Points = g.Select(grp => new ChartPoint
                                                {
                                                    Year = grp.AssessmentYear.Value,
                                                    Month = grp.AssessmentMonth.Value,
                                                    yValue = grp.PercentagePointValue
                                                })
                                     })
        };
}

调用者:

var students =
    db.ClassEnrollments
        .Where(ce => ce.SchoolClass.HomeRoomTeacherID == teacherID)
        .Select(s => s.Student);
var charts = CCProgressChart.GetStudentAssessmentQuestionResultByStudentIdNew(db);
var chartList = students.Select(charts).ToList();

这有什么帮助吗?

最佳答案

.ToList() 实际上是在执行查询。所以您的查询需要 2.5 秒才能运行。

阅读有关延迟执行的更多信息 here .

如果不发布您实际的 LINQ 查询,我们就无法帮助您提高它的性能(如果您发布它,我会更新我的答案)。

关于c# - Linq 查询在几分之一秒内运行,但 .ToList() 需要 3.5 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16812064/

相关文章:

c# - 如何在 Crystal Reports 中抑制交叉表行标题

.net - 如何在 Linq To SQL 中跳过插入/更新时的默认值列?

c# - StructureMap InstanceScope.Hybrid 和 IDisposable

c# - 如何动态执行多个 Linq to Entities 排序

c# - 未获取所有记录或过滤记录

c# - 构建 LINQ 表达式树 : how to get variable in scope

c# - 使 dapper SQL 语法着色

c# - 使用 Linq 不等于

entity-framework - Entity Framework - DRY 查询

c# - 不允许新事务,因为 session 中还有其他线程在运行 LINQ To Entity