public StudentSearchDto SearchStudent(SearchAttribute search) { if (search.PageNumber == 0) { search.PageNumber = 1; } if (search.PageSize == 0) { search.PageSize = 10; } var students = _studentRepository.Records.Search(search.SearchValue); students = students.ApplySort(search.SortString, search.SortOrder); var SearchResult = new StudentSearchDto { PageSize = search.PageSize, TotalPage = students.Count() / search.PageSize + (students.Count() % search.PageSize == 0 ? 0 : 1) }; SearchResult.PageNumber = search.PageNumber > SearchResult.TotalPage ? 1 : search.PageNumber; SearchResult.Students = Mapper.Map <List <Student>, List <StudentDto> >(students.Skip((SearchResult.PageNumber - 1) * SearchResult.PageSize).Take(SearchResult.PageSize).ToList()); return(SearchResult); }
public StudentSearchDto Search(SearchAttribute search) { IEnumerable <Student> students = _studentRepository.Records.Where(x => x.FirstName.Contains(search.SearchValue) || x.LastName.Contains(search.SearchValue) || x.Email.Contains(search.SearchValue)); int studentCount = students.Count(); switch (search.SortString) { case "id": students = (search.SortOrder.ToLower() == "desc") ? students.OrderByDescending(x => x.Id) : students.OrderBy(x => x.Id); break; case "firstName": students = (search.SortOrder.ToLower() == "desc") ? students.OrderByDescending(x => x.FirstName) : students.OrderBy(x => x.FirstName); break; case "lastName": students = (search.SortOrder.ToLower() == "desc") ? students.OrderByDescending(x => x.LastName) : students.OrderBy(x => x.LastName); break; case "dateOfBirth": students = (search.SortOrder.ToLower() == "desc") ? students.OrderByDescending(x => x.DateOfBirth) : students.OrderBy(x => x.DateOfBirth); break; case "email": students = (search.SortOrder.ToLower() == "desc") ? students.OrderByDescending(x => x.Email) : students.OrderBy(x => x.Email); break; default: students = students.OrderBy(x => x.Id); break; } students = students.Skip((search.PageNumber - 1) * search.PageSize).Take(search.PageSize); var result = new StudentSearchDto() { Students = Mapper.Map <IEnumerable <Student>, IEnumerable <StudentDto> >(students), PageNumber = search.PageNumber, TotalPage = studentCount / search.PageSize + (studentCount % search.PageSize == 0 ? 0 : 1), PageSize = search.PageSize }; return(result); }