public async Task <int> Count(Product_MerchantAddressFilter filter)
        {
            IQueryable <Product_MerchantAddressDAO> Product_MerchantAddressDAOs = DataContext.Product_MerchantAddress;

            Product_MerchantAddressDAOs = DynamicFilter(Product_MerchantAddressDAOs, filter);
            return(await Product_MerchantAddressDAOs.CountAsync());
        }
        public async Task <List <Product_MerchantAddress> > List(Product_MerchantAddressFilter filter)
        {
            if (filter == null)
            {
                return(new List <Product_MerchantAddress>());
            }
            IQueryable <Product_MerchantAddressDAO> Product_MerchantAddressDAOs = DataContext.Product_MerchantAddress;

            Product_MerchantAddressDAOs = DynamicFilter(Product_MerchantAddressDAOs, filter);
            Product_MerchantAddressDAOs = DynamicOrder(Product_MerchantAddressDAOs, filter);
            var Product_MerchantAddresss = await DynamicSelect(Product_MerchantAddressDAOs, filter);

            return(Product_MerchantAddresss);
        }
        private async Task <List <Product_MerchantAddress> > DynamicSelect(IQueryable <Product_MerchantAddressDAO> query, Product_MerchantAddressFilter filter)
        {
            List <Product_MerchantAddress> Product_MerchantAddresss = await query.Select(q => new Product_MerchantAddress()
            {
                ProductId         = filter.Selects.Contains(Product_MerchantAddressSelect.Product) ? q.ProductId : default(long),
                MerchantAddressId = filter.Selects.Contains(Product_MerchantAddressSelect.MerchantAddress) ? q.MerchantAddressId : default(long),
                MerchantAddress   = filter.Selects.Contains(Product_MerchantAddressSelect.MerchantAddress) && q.MerchantAddress != null ? new MerchantAddress
                {
                    Id         = q.MerchantAddress.Id,
                    MerchantId = q.MerchantAddress.MerchantId,
                    Code       = q.MerchantAddress.Code,
                    Address    = q.MerchantAddress.Address,
                    Contact    = q.MerchantAddress.Contact,
                    Phone      = q.MerchantAddress.Phone,
                } : null,
                Product = filter.Selects.Contains(Product_MerchantAddressSelect.Product) && q.Product != null ? new Product
                {
                    Id                      = q.Product.Id,
                    Code                    = q.Product.Code,
                    Name                    = q.Product.Name,
                    Description             = q.Product.Description,
                    TypeId                  = q.Product.TypeId,
                    StatusId                = q.Product.StatusId,
                    MerchantId              = q.Product.MerchantId,
                    CategoryId              = q.Product.CategoryId,
                    BrandId                 = q.Product.BrandId,
                    WarrantyPolicy          = q.Product.WarrantyPolicy,
                    ReturnPolicy            = q.Product.ReturnPolicy,
                    ExpiredDate             = q.Product.ExpiredDate,
                    ConditionOfUse          = q.Product.ConditionOfUse,
                    MaximumPurchaseQuantity = q.Product.MaximumPurchaseQuantity,
                } : null,
            }).ToListAsync();

            return(Product_MerchantAddresss);
        }
        private IQueryable <Product_MerchantAddressDAO> DynamicOrder(IQueryable <Product_MerchantAddressDAO> query, Product_MerchantAddressFilter filter)
        {
            switch (filter.OrderType)
            {
            case OrderType.ASC:
                switch (filter.OrderBy)
                {
                case Product_MerchantAddressOrder.Product:
                    query = query.OrderBy(q => q.Product.Id);
                    break;

                case Product_MerchantAddressOrder.MerchantAddress:
                    query = query.OrderBy(q => q.MerchantAddress.Id);
                    break;
                }
                break;

            case OrderType.DESC:
                switch (filter.OrderBy)
                {
                case Product_MerchantAddressOrder.Product:
                    query = query.OrderByDescending(q => q.Product.Id);
                    break;

                case Product_MerchantAddressOrder.MerchantAddress:
                    query = query.OrderByDescending(q => q.MerchantAddress.Id);
                    break;
                }
                break;
            }
            query = query.Skip(filter.Skip).Take(filter.Take);
            return(query);
        }
        private IQueryable <Product_MerchantAddressDAO> DynamicFilter(IQueryable <Product_MerchantAddressDAO> query, Product_MerchantAddressFilter filter)
        {
            if (filter == null)
            {
                return(query.Where(q => false));
            }

            if (filter.ProductId != null)
            {
                query = query.Where(q => q.ProductId, filter.ProductId);
            }
            if (filter.MerchantAddressId != null)
            {
                query = query.Where(q => q.MerchantAddressId, filter.MerchantAddressId);
            }
            return(query);
        }