LINQ to Entity查询执行时间

时间:2013-10-04 11:01:10

标签: c# linq entity-framework

我有这个LINQ to Entity查询,它可以获取大约5000条Plus记录。我注意到,返回的记录越高,执行查询和返回结果所花费的时间就越多。

 lstUserAssignments =  context.UserAssessments.Where(a=>a.UserID == UserID && a.CourseID == ClassID && a.ProductID == a.ProductID && a.AppID == ApplicationID && a.IsDeleted == false && a.Score_Percentage >= 0).Select(a=> new UserAssessmentEntity()
                                          {
                                              ApplicationID = a.AppID,
                                              AttemptNo = a.AttemptNo,
                                              CourseID = a.CourseID,
                                              Score_Percentage = (float)(a.Score_Percentage != null ? a.Score_Percentage : 0),
                                              Status = a.Status,
                                              UserAssessmentID = a.UserAssessmentID,
                                              UserID = a.UserID,
                                              UserScore = a.User_Score,
                                              CreatedDateTime = a.CreatedDateTime,
                                              ModifiedDateTime = a.ModifiedDateTime,
                                              TimeSpent=a.UserAssessmentDetails.Sum(i=>i.TimeSpent??0),
                                              InstructorFeedbackText = a.UserAssessmentDetails.FirstOrDefault()!=null?a.UserAssessmentDetails.FirstOrDefault().InstructorFeedbackText:string.Empty
                                          }).ToList();

使查询变得简单。有UserAssessment和UserAssessmentDetails表。第一个是父母,后者是孩子。查询中存在一些性能问题,我感觉它在计算TimeSpent和InstructorFeedback时。    任何人都可以指出这个问题。

1 个答案:

答案 0 :(得分:0)

Take and Skip:
context.UserAssessments.Where(a=>a.UserID == UserID && a.CourseID == ClassID && a.ProductID == a.ProductID && a.AppID == ApplicationID && a.IsDeleted == false && a.Score_Percentage >= 0).Select(a=> new UserAssessmentEntity()
                                          {
                                              ApplicationID = a.AppID,
                                              AttemptNo = a.AttemptNo,
                                              CourseID = a.CourseID,
                                              Score_Percentage = (float)(a.Score_Percentage != null ? a.Score_Percentage : 0),
                                              Status = a.Status,
                                              UserAssessmentID = a.UserAssessmentID,
                                              UserID = a.UserID,
                                              UserScore = a.User_Score,
                                              CreatedDateTime = a.CreatedDateTime,
                                              ModifiedDateTime = a.ModifiedDateTime,
                                              TimeSpent=a.UserAssessmentDetails.Sum(i=>i.TimeSpent??0),
                                              InstructorFeedbackText = a.UserAssessmentDetails.FirstOrDefault()!=null?a.UserAssessmentDetails.FirstOrDefault().InstructorFeedbackText:string.Empty
                                          }).Take(10).Skip(10).ToList();