public SearchResult getSystemIdAndDateAdvancedSearch(int PageNo, int PageSize, List <SearchItem> SearchItems, int userId, bool hasInspection = false) { var _access = new UserAccess(new SharedContext(), userId); var accessableSystems = _access.GetAccessibleSystems(); var customerSearchIds = SearchItems.Where(item => item.Id == (int)SearchItemType.Customer && item.SearchId != 0).Select(m => m.SearchId).ToList(); if (customerSearchIds.Count() > 0) { accessableSystems = accessableSystems .Where(m => (m.equipmentid_auto != null && customerSearchIds.Contains((int)m.EQUIPMENT.Jobsite.customer_auto)) || (m.crsf_auto != null && customerSearchIds.Contains((int)m.Jobsite.customer_auto)) ); } var jobsiteSearchIds = SearchItems.Where(item => item.Id == (int)SearchItemType.Jobsite && item.SearchId != 0).Select(m => m.SearchId).ToList(); if (jobsiteSearchIds.Count() > 0) { accessableSystems = accessableSystems.Where(m => jobsiteSearchIds.Contains((int)(m.crsf_auto ?? 0))); } var limitedSystems = accessableSystems.ToList(); PageNo = PageNo <= 1 ? 0 : PageNo - 1; using (var _context = new UndercarriageContext()) { var equipmentSearchIds = SearchItems.Where(item => item.Id == (int)SearchItemType.Equipment && item.SearchId != 0).Select(m => m.SearchId).ToList(); if (equipmentSearchIds.Count() > 0) { limitedSystems = limitedSystems.Where(m => equipmentSearchIds.Contains((int)(m.equipmentid_auto ?? 0))).ToList(); } //Family var familySearchIds = SearchItems.Where(item => item.Id == (int)SearchItemType.Family && item.SearchId != 0).Select(m => m.SearchId).ToList(); if (familySearchIds.Count() > 0) { limitedSystems = limitedSystems .Where(m => m.make_auto != null) .Where(m => familySearchIds.Contains(m.Make.LU_MMTA.FirstOrDefault().type_auto)).ToList(); } //Make var makeSearchIds = SearchItems.Where(item => item.Id == (int)SearchItemType.Make && item.SearchId != 0).Select(m => m.SearchId).ToList(); if (makeSearchIds.Count() > 0) { limitedSystems = limitedSystems.Where(m => makeSearchIds.Contains(m.make_auto ?? 0)).ToList(); } //Model var modelSearchIds = SearchItems.Where(item => item.Id == (int)SearchItemType.Model && item.SearchId != 0).Select(m => m.SearchId).ToList(); if (modelSearchIds.Count() > 0) { limitedSystems = limitedSystems.Where(m => modelSearchIds.Contains(m.model_auto ?? 0)).ToList(); } var evaluationExcludedA = SearchItems.Where(item => item.Id == (int)SearchItemType.EvaluationA && item.SearchId == 1).Count() == 0; /*if (evaluationExcludedA) { * limitedSystems = limitedSystems.Where(m => * m.TRACK_INSPECTION_DETAIL.OrderByDescending(order => order.TRACK_INSPECTION.inspection_date).FirstOrDefault().worn_percentage.Max(k => k.worn) > (int)WornLimit.A).ToList(); * * var evaluationExcludedB = SearchItems.Where(item => item.Id == (int)SearchItemType.EvaluationB && item.SearchId == 1).Count() == 0; * if (evaluationExcludedB) * limitedEquipments = limitedEquipments.Where(m => * m.Inspections.OrderByDescending(order => order.InspectionDate).FirstOrDefault().Details.Max(k => k.worn) <= (int)WornLimit.A || * m.Inspections.OrderByDescending(order => order.InspectionDate).FirstOrDefault().Details.Max(k => k.worn) > (int)WornLimit.B).ToList(); * * var evaluationExcludedC = SearchItems.Where(item => item.Id == (int)SearchItemType.EvaluationC && item.SearchId == 1).Count() == 0; * if (evaluationExcludedC) * limitedEquipments = limitedEquipments.Where(m => * m.Inspections.OrderByDescending(order => order.InspectionDate).FirstOrDefault().Details.Max(k => k.worn) <= (int)WornLimit.B || * m.Inspections.OrderByDescending(order => order.InspectionDate).FirstOrDefault().Details.Max(k => k.worn) > (int)WornLimit.C).ToList(); * * var evaluationExcludedX = SearchItems.Where(item => item.Id == (int)SearchItemType.EvaluationX && item.SearchId == 1).Count() == 0; * if (evaluationExcludedX) * limitedEquipments = limitedEquipments.Where(m => * m.Inspections.OrderByDescending(order => order.InspectionDate).FirstOrDefault().Details.Max(k => k.worn) <= (int)WornLimit.C).ToList(); */ var IdAndDates = limitedSystems.Select(m => new IdAndDate { Id = (int)m.Module_sub_auto, Date = DateTime.Now }); return(new SearchResult { Total = IdAndDates.Count(), Result = IdAndDates.OrderByDescending(m => m.Date).Skip(PageNo * PageSize).Take(PageSize).ToList().Select(m => new IdAndDate { Id = m.Id, Date = m.Date.ToLocalTime().Date }).ToList() }); } }