public IHttpActionResult Sort(SortBindingModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var user = unitOfWork.AppUsers.FirstOrDefault(u => u.Email == User.Identity.Name && u.Deleted == false); if (user == null) { return(BadRequest()); } List <Ride> results = new List <Ride>(); List <Ride> temp = new List <Ride>(); if (model.SortBy == "Date") { results = model.ResultList.OrderBy(r => r.OrderDT).ToList(); } if (model.SortBy == "Rating") { List <Ride> noRatingRides = new List <Ride>(); List <Ride> yesRatingRides = new List <Ride>(); foreach (Ride r in model.ResultList) { if (r.UserComment.Count() == 0) { noRatingRides.Add(r); } else { yesRatingRides.Add(r); } } temp = yesRatingRides.OrderBy(r => r.UserComment.First().Rating).ToList(); if (noRatingRides.Count() != 0) { results = noRatingRides; results.AddRange(temp); } else { results = temp; } } return(Ok(results)); }
public CategoryArticleViewModel GetSorted_CAM(CategoriesInformation.CategoriesEnum category, SortBindingModel model) { var converted = GetStringCategoryFromEnum(category); var returnModel = new CategoryArticleViewModel(); IEnumerable <ArticleDataModel> queryModel = context.Articles .Where(a => a.Category == converted); System.Diagnostics.Debug.WriteLine(string.Format("Search => {0} / Order => {1} / PriceF => {2} / PriceT => {3} / Category => {4} / querymodelISNULL = {5} / Type = {6}", model.Search != null ? model.Search : "NULL", model.SortBy, model.PriceLimitationFrom, model.PriceLimitationTo, converted, queryModel != null ? "No" : "Yes", model.Type)); #region querryModel != null if (queryModel != null) { if (model.Type != null && model.Type != "Any") { IEnumerable <ArticleDataModel> temp; temp = queryModel .Where(a => a.Type == model.Type); if (temp != null) { queryModel = temp; } } if (model.Search != null && model.Search != string.Empty) { IEnumerable <ArticleDataModel> temp; temp = queryModel .Where(a => a.Name.Contains(model.Search)); if (temp != null && temp.Count() > 0) { queryModel = temp; } } switch (model.SortBy) { case "Most recent": { queryModel = queryModel .OrderByDescending(a => a.UploadDate); } break; case "Price (ascending)": { queryModel = queryModel .OrderBy(a => a.Price); } break; case "Price (descending)": { queryModel = queryModel .OrderByDescending(a => a.Price); } break; } if (model.PriceLimitationTo != 0) { queryModel = queryModel .Where(a => (a.Price >= model.PriceLimitationFrom && a.Price <= model.PriceLimitationTo)); } } #endregion var mapped = MapArticles(queryModel); returnModel.PreviewModels = mapped; returnModel.SortBar = new SortBindingModel() { SortByValues = new string[] { "Most recent", "Price (ascending)", "Price (descending)" } }; return(returnModel); }