Sha*_*ica 5 c# linq linq-to-entities entity-framework-5
我正在运行一个Linq查询,返回大约25条记录,每条记录有10个数字列.根据我的代码分析器,查询本身只需要几分之一秒 - 但调用.ToList()大约需要3.5秒.如上所述,从SQL返回的数据量是微不足道的,因此将其复制到a中所花费的时间List不应该是繁重的.
为什么.ToList()要这么久?怎么可以改进?
编辑:赞赏所有快速答案,让我更清楚地说明:我完全意识到查询是延迟加载的事实.我看到的现象是,SQL Server Profiler和ANTS Performance Profiler都报告实际查询执行时间只有几分之一秒.
这是ANTS的屏幕截图:

请注意,调用方法需要4.3秒,而实际的SQL查询都不会超过.05秒.可能是该方法中的其他代码,而不是SQL?让我们看看ANTS如何在这里分解代码配置文件:

吸烟枪证明:.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
})
})
};
}
Run Code Online (Sandbox Code Playgroud)
这称为:
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();
Run Code Online (Sandbox Code Playgroud)
这对你有帮助吗?
| 归档时间: |
|
| 查看次数: |
1927 次 |
| 最近记录: |