public IActionResult Index(int?Page, string SortExpression, bool?Accending, List <int> Genres) { //Set default values for all optional parameters var safePage = Page ?? 1; var safeSortExpression = string.IsNullOrEmpty(SortExpression) ? "CreateDate" : SortExpression; bool useDefaultSort = Accending.HasValue ? Accending.Value : true; var resultsFound = 0; var model = new CollaborationSpaceSearchResultModel(); var search = new CollaborationSpaceSearchParams { SortExpression = safeSortExpression, CurrentPageNumber = safePage, ItemsPerPage = 10, GenreFilter = Genres }; model.CollaborationSpaceSearchResults = _UnitOfWork.CollaborationSpaceRepository.GetActiveCollaborationSpaces(search, out resultsFound); model.NumberOfResults = resultsFound; model.GenreLookUpList = (IList <GenreLookUp>)_UnitOfWork.GenreLookUpRepository.Query(); model.CurrentPage = safePage; model.ItemsPerPage = 10; model.SortExpression = safeSortExpression; // if a filter has been selected add to the model // so we can show what filter is selected in the view if (Genres != null && Genres.Count > 0) { model.GenreLookUpId = Genres[0]; } model.ResultsDescription = string.Format("Displaying records {0} - {1} of {2} sorted by {3}", (safePage * 10), (safePage * 10) + 10, resultsFound, safeSortExpression); return(View("Index", model)); }
public List <CollaborationSpaceSearchResult> GetActiveCollaborationSpaces(CollaborationSpaceSearchParams filter, out int resultsFound) { if (filter == null) { throw new ArgumentNullException("filter"); } var collabSpacesQuery = from a in _context.CollaborationSpaces join o in _context.CollaborationSpaceGenres on a.CollaborationSpaceId equals o.CollaborationSpaceId join p in _context.ArtistCollaborationSpaces on a.CollaborationSpaceId equals p.CollaborationSpaceId join artist in _context.Artists on p.ArtistId equals artist.ArtistId where a.Status != ProjectStatus.Canceled && a.Status != ProjectStatus.OnHold && a.Status != ProjectStatus.Published && a.AllowPublicView == true && p.IsCreator == true select new CollaborationSpaceSearchResult() { CollaborationSpaceId = a.CollaborationSpaceId, CreateDate = a.CreateDate, Description = a.Description, LastPostDate = a.LastPostDate, ModifiedDate = a.ModifiedDate, NumberComments = a.NumberComments, NumberViews = a.NumberViews, RestrictContributorsToBand = a.RestrictContributorsToBand, Status = a.Status, Title = a.Title, GenreLookUpId = o.GenreLookUpId, UserName = artist.UserName, WebSite = artist.WebSite, AvatarURL = artist.AvatarUrl }; if (filter.GenreFilter != null && filter.GenreFilter.Count > 0) { collabSpacesQuery = collabSpacesQuery.Where(LinqUtilities.BuildOrExpression <CollaborationSpaceSearchResult, int>(p => p.GenreLookUpId, filter.GenreFilter)); } //get rid of duplicates // EF Bug causing invalid caste exception // https://github.com/aspnet/EntityFramework/issues/1909 //collabSpacesQuery = (from a in collabSpacesQuery // group a by a.CollaborationSpaceId into u // select u.First()); // First round trip to the database that runs a query to // get the count resultsFound = collabSpacesQuery == null ? 0 : collabSpacesQuery.Count(); int skip = getSkip(filter.CurrentPageNumber, filter.ItemsPerPage); //several EF bugs causing incorrect results here // https://github.com/aspnet/EntityFramework/issues/1851 collabSpacesQuery = collabSpacesQuery.OrderByText(filter.SortExpression).Skip(skip).Take(filter.ItemsPerPage); // second round trip to the database retrieves (count) 10 records return(collabSpacesQuery == null ? null : collabSpacesQuery.ToList()); }