如何提高linq的性能?

时间:2019-09-17 01:13:01

标签: performance

我在数据库上有超过60,000条记录。而且我想知道如何在执行查询时提高性能。

这是我的代码:

  public IEnumerable<SalesProWithTaskModel> GetSalesPro(int storeId, int filter, DateTime? startDate, DateTime? endDate)
        {
            var salesproStats = GetFilteredSalesPro(storeId, filter, startDate, endDate);

            return salesproStats.Where(d => d.IncompleteTasks > 0 || d.CompleteTasks > 0)
                    .OrderBy(d => d.FirstName)
                    .Select(d => new SalesProWithTaskModel
                    {
                        Id = d.Id,
                        FirstName = d.FirstName,
                        LastName = d.LastName
                    }).ToList();
        }

上面的代码是将根据其过滤器获得所有salespro的代码。

 private IEnumerable<SalesProStats> GetFilteredSalesPro(int storeId, int filter, DateTime? startDate, DateTime? endDate)
        {

            var currentDate = DateTime.Now;

            var range = _timeProvider.CreateDateRangeFromDate(currentDate);

            var allSalesPro = new List<SalesProStats>();

            switch (filter)
            {
                case (int)GraphFiltersEnum.Today:
                    range.From = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day);
                    allSalesPro = AddSalesProToList(storeId, filter, range);
                    break;

                case (int)GraphFiltersEnum.Yesterday:
                    range.From = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day).AddDays(-1);
                    allSalesPro = AddSalesProToList(storeId, filter, range);
                    break;
              }

            return allSalesPro;
        }

上面的代码显示了要使用的过滤器。

 private List<SalesProStats> AddSalesProToList(int storeId, int filter, DateTimeFromTo dateRange)
        {
            var activeStoreSalespro = GetActiveStoreSalesPro(storeId);

            var allSalesPro = new List<SalesProStats>();

            foreach (var storeSalesPro in activeStoreSalespro)
            {
                var salesPro = GetSalesProStatsDetails(storeId, storeSalesPro, filter, dateRange);

                allSalesPro.Add(salesPro);
            }

            return allSalesPro;
        }

上面的代码是根据salesPro的过滤器进行迭代的代码。

  private IEnumerable<MacHaik.Data.User> GetActiveStoreSalesPro(int storeId)
        {
            var activeSalesPro = _userService.GetActiveUsersByStore().Where(d => d.IsSalesPro == true);

            var activeStoreSalesPro = activeSalesPro.Where(c => c.StoreUsers.Any(d => d.StoreId == storeId));

            return activeStoreSalesPro;
        }

上面的代码是我的GetActiveStoresSalesPro。

 private SalesProStats GetSalesProStatsDetails(int storeId, MacHaik.Data.User salesPro, int filter, DateTimeFromTo dateRange)
        {
            var totalTasks = GetSalesProAssignedTasksCount(storeId, salesPro.Id, dateRange);

            var completeTasks = GetSalesProCompletedTasksCount(storeId, salesPro.Id, dateRange);

            var salesProStat = new SalesProStats
            {
                Id = salesPro.Id,
                FirstName = salesPro.FirstName,
                LastName = salesPro.LastName,
                GraphMode = filter,
                StartDate = dateRange.From,
                EndDate = dateRange.To,
                TotalTasks = totalTasks,
                CompleteTasks = completeTasks,
                IncompleteTasks = GetSalesProInCompletedTasksCount(storeId, salesPro.Id, dateRange)
            };

           salesProStat.CompletedTasksPercentage = totalTasks > 0 ? Math.Round((decimal)(completeTasks) / (totalTasks) * 100, 2) : 0;

            return salesProStat;
        }

上面的代码是获取totalTask​​,completeTasks和incompleteTasks的查询

这是我的totalTask​​的查询

  private int GetSalesProAssignedTasksCount(int storeId, int userId, DateTimeFromTo dateRange)
        {
            var runningTaskStatusId = (int)AssignedTaskStatusEnum.Running;

            var completedTaskStatusId = (int)AssignedTaskStatusEnum.Completed;

            return _dbContext.CustomerAssignedTaskExecs.Count(d =>
                d.CustomerAssignedTask.Customer.StoreId == storeId &&
                (dateRange.From <= d.ScheduledDispatchedDateTime &&
                d.ScheduledDispatchedDateTime <= dateRange.To) &&
                (d.CustomerAssignedTask.Prospect.Prospect_Details.SalesPro1 == userId ||
                d.CustomerAssignedTask.Prospect.Prospect_Details.SalesPro2 == userId) &&
                (d.CustomerAssignedTask.TaskStatusId == completedTaskStatusId ||
                d.CustomerAssignedTask.TaskStatusId == runningTaskStatusId));
        }

这是我对completedTask的查询

     private IEnumerable<CustomerAssignedTaskExec> GetCompletedTaskExecs(int storeId, int userId, DateTimeFromTo dateRange)
        {
            return _dbContext.CustomerAssignedTaskExecs.Where(d => d.CustomerAssignedTask.Customer.StoreId == storeId &&
                (dateRange.From <= d.ScheduledDispatchedDateTime &&
                d.ScheduledDispatchedDateTime <= dateRange.To) &&
                (d.CustomerAssignedTask.Prospect.Prospect_Details.SalesPro1 == userId ||
                d.CustomerAssignedTask.Prospect.Prospect_Details.SalesPro2 == userId) &&
                d.CustomerAssignedTask.TaskStatusId != (int)AssignedTaskStatusEnum.Cancelled &&
                d.IsProcessed == true
                );
        }

如果我有30,000条记录,则检索数据的执行时间非常慢。如何改善呢?谢谢

0 个答案:

没有答案