public IActionResult GetAllPaginatedWithDetail([FromQuery] GetAllPaginatedRequestModel requestModel, [FromHeader] string displayLanguage) { var responseModel = new Return <PaginatedList <LocationMoveWithDetail> >(); responseModel.DisplayLanguage = displayLanguage; try { var searchFilter = new LocationMoveSearchFilter(); searchFilter.CurrentPage = requestModel.CurrentPage.HasValue ? requestModel.CurrentPage.Value : 1; searchFilter.PageSize = requestModel.PageSize.HasValue ? requestModel.PageSize.Value : 10; searchFilter.SortOn = requestModel.SortOn; searchFilter.SortDirection = requestModel.SortDirection; searchFilter.Filter_Car_Plate = requestModel.Plate; searchFilter.Filter_Location_Name = requestModel.LocationName; responseModel.Data = _locationMoveService.GetAllPaginatedWithDetailBySearchFilter(searchFilter); responseModel.Status = ResultStatusCodeStatic.Success; responseModel.Message = "Success"; responseModel.Success = true; return(Ok(responseModel)); } catch (Exception ex) { responseModel.Status = ResultStatusCodeStatic.InternalServerError; responseModel.Message = "An error occurred"; responseModel.Success = false; ReturnError error = new ReturnError(); error.Key = "500"; error.Message = ex.Message; error.Code = 500; responseModel.Errors = new List <ReturnError>(); responseModel.Errors.Add(error); responseModel.Data = null; //hata oluştugundan dolayı Data null olarak dönülür. return(StatusCode(StatusCodes.Status500InternalServerError, responseModel)); } }
public PaginatedList <LocationMoveWithDetail> GetAllPaginatedWithDetailBySearchFilter(LocationMoveSearchFilter searchFilter) { PaginatedList <LocationMoveWithDetail> resultList = new PaginatedList <LocationMoveWithDetail>(new List <LocationMoveWithDetail>(), 0, searchFilter.CurrentPage, searchFilter.PageSize, searchFilter.SortOn, searchFilter.SortDirection); using (AppDBContext dbContext = new AppDBContext(_config)) { var query = from lm in dbContext.LocationMove from l in dbContext.Location.Where(x => x.Id == lm.LocationId).DefaultIfEmpty() from c in dbContext.Car.Where(x => x.Id == lm.CarId).DefaultIfEmpty() select new LocationMoveWithDetail() { Id = lm.Id, CarId = lm.CarId, LocationId = lm.LocationId, EntryDate = lm.EntryDate, ExitDate = lm.ExitDate, Car_Plate = c == null ? String.Empty : c.Plate, Location_Name = l == null ? String.Empty : l.Name }; // filtering if (!string.IsNullOrEmpty(searchFilter.Filter_Car_Plate)) { query = query.Where(r => r.Car_Plate.Contains(searchFilter.Filter_Car_Plate)); } if (!string.IsNullOrEmpty(searchFilter.Filter_Location_Name)) { query = query.Where(r => r.Location_Name.Contains(searchFilter.Filter_Location_Name)); } // asnotracking query = query.AsNoTracking(); //total count var totalCount = query.Count(); //sorting if (!string.IsNullOrEmpty(searchFilter.SortOn)) { // using System.Linq.Dynamic.Core; nuget paketi ve namespace eklenmelidir, dynamic order by yapmak icindir query = query.OrderBy(searchFilter.SortOn + " " + searchFilter.SortDirection.ToUpper()); } else { // deefault sıralama vermek gerekiyor yoksa skip metodu hata veriyor ef 6'da -- 28.10.2019 15:40 // https://stackoverflow.com/questions/3437178/the-method-skip-is-only-supported-for-sorted-input-in-linq-to-entities query = query.OrderBy(r => r.Id); } //paging query = query.Skip((searchFilter.CurrentPage - 1) * searchFilter.PageSize).Take(searchFilter.PageSize); resultList = new PaginatedList <LocationMoveWithDetail>( query.ToList(), totalCount, searchFilter.CurrentPage, searchFilter.PageSize, searchFilter.SortOn, searchFilter.SortDirection ); } return(resultList); }