public ActionResult List(ProductSearchOption search, PagerRequest request)
 {
     int totalCount;
     var vo = internalSearch(search, request,out totalCount);
     var v = new ProductCollectionViewModel(request, totalCount) { Products = vo.ToList() };
     ViewBag.SearchOptions = search;
     return View(v);
 }
        public PartialViewResult Select(ProductSearchOption search, PagerRequest request)
        {
            int totalCount;
            search.CurrentUser = CurrentUser.CustomerId;
            search.CurrentUserRole = CurrentUser.Role;
            var dbContext = _productRepository.Context;
            var linq = dbContext.Set<ProductEntity>().Where(p => (!search.PId.HasValue || p.Id == search.PId.Value) &&
                (string.IsNullOrEmpty(search.Name) || p.Name.StartsWith(search.Name)) &&
                (!search.User.HasValue || p.CreatedUser == search.User.Value) &&
                (!search.Status.HasValue || p.Status == (int)search.Status.Value) &&
                p.Status != (int)DataStatus.Deleted);
            linq = _userAuthRepo.AuthFilter(linq, search.CurrentUser, search.CurrentUserRole) as IQueryable<ProductEntity>;
            if (!string.IsNullOrEmpty(search.Topic) &&
                 search.Topic.Trim().Length > 0)
            {
                linq = linq.Where(p => (from s in dbContext.Set<SpecialTopicEntity>()
                                        join ps in dbContext.Set<SpecialTopicProductRelationEntity>() on s.Id equals ps.SpecialTopic_Id
                                        where s.Name.StartsWith(search.Topic) && ps.Product_Id == p.Id
                                        select s).Any());
            }
            if (!string.IsNullOrEmpty(search.Promotion) &&
                search.Promotion.Trim().Length > 0)
            {
                linq = linq.Where(p => (from pr in dbContext.Set<PromotionEntity>()
                                        join ps in dbContext.Set<Promotion2ProductEntity>() on pr.Id equals ps.ProId
                                        where pr.Name.StartsWith(search.Promotion) && ps.ProdId == p.Id
                                        select pr).Any());

            }
            var linq2 = linq.Join(dbContext.Set<StoreEntity>().Where(s => string.IsNullOrEmpty(search.Store) || s.Name.StartsWith(search.Store)), o => o.Store_Id, i => i.Id, (o, i) => new { P = o, S = i })
               .Join(dbContext.Set<BrandEntity>().Where(b => string.IsNullOrEmpty(search.Brand) || b.Name.StartsWith(search.Brand)), o => o.P.Brand_Id, i => i.Id, (o, i) => new { P = o.P, S = o.S, B = i })
               .Join(dbContext.Set<UserEntity>(), o => o.P.CreatedUser, i => i.Id, (o, i) => new { P = o.P, S = o.S, B = o.B, C = i })
               .Join(dbContext.Set<TagEntity>().Where(t => string.IsNullOrEmpty(search.Tag) || t.Name.StartsWith(search.Tag)), o => o.P.Tag_Id, i => i.Id, (o, i) => new { P = o.P, S = o.S, B = o.B, C = o.C, T = i })
                .GroupJoin(dbContext.Set<ResourceEntity>().Where(r => r.SourceType == (int)SourceType.Product), o => o.P.Id, i => i.SourceId
                           , (o, i) => new { P = o.P, S = o.S, B = o.B, C = o.C, T = o.T, R = i });


            linq2 = linq2.OrderByDescending(l => l.P.CreatedDate);
           
            totalCount = linq2.Count();

            var skipCount = (request.PageIndex - 1) * request.PageSize;

            linq2 = skipCount == 0 ? linq2.Take(request.PageSize) : linq2.Skip(skipCount).Take(request.PageSize);


            var vo = from l in linq2.ToList()
                     select new ProductViewModel().FromEntity<ProductViewModel>(l.P, p =>
                     {
                         p.StoreName = l.S.Name;
                         p.TagName = l.T.Name;
                         p.BrandName = l.B.Name;
                         p.CreateUserName = l.C.Nickname;
                        
                         p.Resources = l.R.Select(r => new ResourceViewModel().FromEntity<ResourceViewModel>(r));
                     });

           

            var v = new ProductCollectionViewModel(request, totalCount) { Products = vo.ToList() };
            ViewBag.SearchOptions = search;
            return PartialView("_Selector", v);
        }