public SortableBindingList <AutoPartDisplayModel> FetchAutoPartWithSearch(AutoPartFilterModel filter) { try { var query = CreateQuery(filter); var result = from a in query select new AutoPartDisplayModel { Id = a.Id, PartName = a.AutoPart.PartName, Description = a.Description != string.Empty ? a.Description : "-", BrandName = a.Brand.BrandName, Make = a.Make != string.Empty ? a.Make : "-", Model = a.Model != string.Empty ? a.Model : "-", Quantity = a.Quantity.HasValue ? a.Quantity.Value : 0, PartNumber = a.PartNumber, Unit = a.Unit != string.Empty ? a.Unit : "-", RetailPrice = a.SellingPrice1.HasValue ? a.SellingPrice1.Value : 0, PurchasePrice = a.BuyingPrice.HasValue ? a.BuyingPrice.Value : 0, Size = a.Size != string.Empty ? a.Size : "-" }; SortableBindingList <AutoPartDisplayModel> b = new SortableBindingList <AutoPartDisplayModel>(result); return(b); } catch (Exception ex) { throw ex; } }
private IQueryable <AutoPartDetail> CreateQuery(AutoPartFilterModel filter) { var items = from i in db.AutoPartDetail where i.IsDeleted == false select i; if (filter != null) { if (filter.AutoPartId != 0) { items = items.Where(a => a.AutoPartId == filter.AutoPartId); } if (!string.IsNullOrWhiteSpace(filter.PartNumber)) { items = items.Where(a => a.PartNumber.Contains(filter.PartNumber) || a.AutoPartAltPartNumber.Any(b => b.AltPartNumber.Contains(filter.PartNumber))); } if (filter.BrandId != 0) { items = items.Where(a => a.BrandId == filter.BrandId); } if (!string.IsNullOrWhiteSpace(filter.Model)) { items = items.Where(a => a.Model.Contains(filter.Model)); } if (!string.IsNullOrWhiteSpace(filter.Size)) { items = items.Where(a => a.Size.Contains(filter.Size)); } if (filter.ForReorder) { items = items.Where(a => a.Quantity <= a.ReorderLimit && a.Quantity > 0); DateTime reorderWindowDate = DateTime.Now.AddDays(-4).Date; if (filter.ReorderList) { items = items.Where(a => (a.ReorderDate < reorderWindowDate || a.ReorderDate.HasValue == false)); } else { items = items.Where(a => (a.ReorderDate >= reorderWindowDate)); } } } return(items); }