// GET: Questions public async Task <IActionResult> Index(string locationId, string search) { // Use LINQ to get list of genres. IQueryable <int> locationQuery = from location in _locationContext.Location orderby location.Name select location.LocationId; var questions = from m in _questionContext.Question select m; if (!string.IsNullOrEmpty(search)) { questions = questions.Where(s => s.Content.Contains(search)); } if (!string.IsNullOrEmpty(locationId)) { questions = questions.Where(x => x.LocationId == int.Parse(locationId)); } var vm = new QuestionLocationViewModel { Locations = new SelectList(await locationQuery.Distinct().ToListAsync()), Questions = await questions.ToListAsync() }; return(View(vm)); }
public async Task <IViewComponentResult> InvokeAsync(string locationId, string search) { IQueryable <int> locationQuery = from location in _locationContext.Location orderby location.Name select location.LocationId; var questions = from m in _questionContext.Question select m; var answers = from m in _answerContext.Answer select m; if (!string.IsNullOrEmpty(search)) { questions = questions.Where(s => s.Content.Contains(search)); } if (!string.IsNullOrEmpty(locationId)) { questions = questions.Where(x => x.LocationId == int.Parse(locationId)); } foreach (var question in questions) { question.Answers = await answers.Where(x => x.QuestionId == question.QuestionId).ToListAsync(); } var vm = new QuestionLocationViewModel { LocationId = locationId, Search = search, Locations = new SelectList(await locationQuery.Distinct().ToListAsync()), Questions = await questions.ToListAsync() }; return(View(vm)); }