如何解决在.Net MVC
我有一个生成工作单列表的函数:
public async Task<Object> GetAllWorkOrdersForUserAsync(string from,string to,int start,int length)
{
IQueryable<ReportWorkOrderViewModel> resp;
resp = (from wo in _dbContext.WorkOrders
join woc in _dbContext.WorkOrderCategory on wo.WorkOrderCategoryId equals woc.Id
join sc in _dbContext.ServiceClients on wo.ServiceClientId equals sc.Id
join usr in _dbContext.Users on wo.UserId equals usr.Id into user
from uur in user.DefaultIfEmpty()
join contractor in _dbContext.Users on wo.ContractorId equals contractor.Id into contr
from contor in contr.DefaultIfEmpty()
select new ReportWorkOrderViewModel
{
Id = wo.Id,WorkOrderType = woc.Name,CustomerName = sc.FirstName + " " + sc.LastName,CustomerAccount = wo.ServiceClientId,AppointmentDate = wo.StartDateTime.ToString(),IsHiring = (userId == wo.UserId),CreatedAtDate = wo.CreatedAt.ToString(),CreatedAtTime = wo.CreatedAt.ToString(),CreatedBy = uur != null ? (uur.CompanyName != string.Empty ? uur.CompanyName : uur.ContactFirst + " " + uur.ContactLast) : string.Empty,AssignedTechnician = contor != null ? (contor.ContactFirst + " " + contor.ContactLast) : string.Empty,AppointmentTime = wo.StartDateTime.ToString(),City = sc.City,State = sc.State,TicketStatus = wo.Status,CompletionNote = wo.WorkPerformedDetail,ClientsNote = wo.CustomerNotes,TechStatus = wo.TechStatus == TechnicianStatus.Arrived ? "Arrivied" : wo.TechStatus == TechnicianStatus.OnTheWay ? "On The Way" : "Init",});
int totalRows = resp.Count();
if (!string.IsNullOrEmpty(from) && !string.IsNullOrEmpty(to))
{
try
{
DateTime fromDate = DateTime.Parse(from);
DateTime toDate = DateTime.Parse(to);
resp = resp.AsEnumerable().Where(d => Convert.ToDateTime(d.AppointmentDate) >= fromDate && Convert.ToDateTime(d.AppointmentDate) <= toDate).AsQueryable();
}
catch
{
}
}
int totalRowsAfterFiltering = resp.Count();
//<!--sorting-->
resp = resp.OrderBy(searchColumnName + " " + sortDirection);
//<!--paging-->
if (paginate)
{
if (totalRowsAfterFiltering != totalRows)
{
resp = resp.Skip(start).Take(length);
}
else
{
resp = resp.Where(x => x.Id >= start).Take(length);
}
}
var response = new
{
data = resp,recordsTotal = totalRows,recordsFiltered = totalRowsAfterFiltering
};
return response;
}
我的ViewModel是
public class ReportWorkOrderViewModel
{
public int Id { get; set; }
public string WorkOrderType { get; set; }
public int CustomerAccount { get; set; }
public string CustomerName { get; set; }
private string appointmentDate;
public string AppointmentDate { get => appointmentDate; set => appointmentDate = Convert.ToDateTime(value).ToString("MM/dd/yyyy"); }
private string appointmentTime;
public string AppointmentTime { get => appointmentTime; set => appointmentTime = Convert.ToDateTime(value).ToString("HH:mm tt"); }
public string CreatedBy { get; set; }
}
当From和To没有值时,上面的函数可以正常工作并生成所有列表。列表中的总数为6-7000,但是当我需要通过日期和时间进行数据过滤的列表时。我的系统在这部分代码中停留在int totalRowsAfterFiltering = resp.Count();
中15到20秒,最后仅填充了2/3数据。但是,当我不按日期筛选时,列表将在2秒内执行。这是什么问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。