コード例 #1
0
        public HouseSearchResult Search(HouseSearchOptions options)
        {
            using (RhDbContext ctx = new RhDbContext())
            {
                CommonService <HouseEntity> cs = new CommonService <HouseEntity>(ctx);
                //判断所有的searchoptions
                var items = cs.GetAll().Where(t => t.Community.Region.CityId == options.CityId);

                items = items.Where(t => t.TypeId == options.TypeId);


                if (options.RegionId != null)
                {
                    items = items.Where(t => t.Community.RegionId == options.RegionId);
                }

                if (options.StartMonthRent != null)
                {
                    items = items.Where(t => t.MonthRent >= options.StartMonthRent);
                }

                if (options.EndMonthRent != null)
                {
                    items = items.Where(t => t.MonthRent <= options.EndMonthRent);
                }

                if (!string.IsNullOrEmpty(options.Keywords))
                {
                    items = items.Where(t => t.Address.Contains(options.Keywords) ||
                                        t.Description.Contains(options.Keywords) ||
                                        t.Community.Name.Contains(options.Keywords) ||
                                        t.Community.Location.Contains(options.Keywords) ||
                                        t.Community.Traffic.Contains(options.Keywords));
                }
                //使用include连接查询,是立即把外键表都查询出来,正常是用到外键表才去查的,但是这里已知肯定要用到外键表,所以用include,避免延迟加载,不然查询的时候一直去查外键表肯定不好
                items = items.Include(h => h.Attachments).Include(h => h.Community)
                        .Include(h => h.Community.Region).Include(h => h.Community.Region.City).Include(h => h.Type);
                //搜索结果总条数
                long totalCount = items.LongCount();
                switch (options.OrderByType)
                {
                case HouseSearchOrderByType.AreaAsc:
                    items = items.OrderBy(t => t.Area);
                    break;

                case HouseSearchOrderByType.AreaDesc:
                    items = items.OrderByDescending(t => t.Area);
                    break;

                case HouseSearchOrderByType.MonthRentAsc:
                    items = items.OrderBy(t => t.MonthRent);
                    break;

                case HouseSearchOrderByType.MonthRentDesc:
                    items = items.OrderByDescending(t => t.MonthRent);
                    break;

                case HouseSearchOrderByType.CreateDateDesc:
                    items = items.OrderBy(t => t.CreateDateTime);
                    break;
                }
                //一定不要items.Where
                //而要items=items.Where();
                //OrderBy要在Skip和Take之前
                //给用户看的页码从1开始,程序中是从0开始
                items = items.Skip((options.CurrentIndex - 1) * options.PageSize).Take(options.PageSize);
                HouseSearchResult searchResult = new HouseSearchResult();
                searchResult.totalCount = totalCount;
                HouseEntity[]   lastItems = items.ToArray();
                List <HouseDTO> houses    = new List <HouseDTO>();
                foreach (var houseEntity in lastItems)
                {
                    houses.Add(Entity2DTO(houseEntity));
                }

                searchResult.result = houses.ToArray();
                return(searchResult);
            }
        }