예제 #1
0
        //gets a HouseViewModel for every house in the selected county
        public async static Task <HomeIndexViewModel> getHomeIndexViewModel(AppDbContext _context, string county)
        {
            HomeIndexViewModel HomeIndexVM = new HomeIndexViewModel();

            //county query
            IQueryable <string> countyQuery = from h in _context.House
                                              orderby h.County
                                              select h.County;

            //house query
            var housesQuery = from h in _context.House
                              select h;

            //where house county == county
            if (county != null && county != "")
            {
                housesQuery = housesQuery.Where(h => h.County == county);
            }

            //get houses
            var houses = await housesQuery.ToListAsync();

            foreach (House property in houses)
            {
                //get images for house
                var imagesQuery = from i in _context.Image
                                  select i;
                imagesQuery = imagesQuery.Where(i => i.HouseId == property.Id);

                //create house view model
                HouseViewModel HouseVM = new HouseViewModel
                {
                    //house = await housesQuery.FirstAsync(),
                    house  = property,
                    Images = await imagesQuery.ToListAsync()
                };

                //add house to home index view model
                HomeIndexVM.Houses.Add(HouseVM);

                //add counties to home index view model
                HomeIndexVM.County = new SelectList(await countyQuery.Distinct().ToListAsync());
            }

            return(HomeIndexVM);
        }
예제 #2
0
        //get house by id
        //get all houses by houseId
        //returns HouseViewModel
        public async static Task <HouseViewModel> getHouse(AppDbContext _context, int id)
        {
            //get house query
            var houseQuery = from h in _context.House
                             select h;

            houseQuery = houseQuery.Where(h => h.Id == id);

            //get images query
            var images = from i in _context.Image
                         select i;

            images = images.Where(i => i.HouseId == id);

            var HouseVM = new HouseViewModel
            {
                house  = await houseQuery.FirstAsync(),
                Images = await images.ToListAsync()
            };

            return(HouseVM);
        }