public async Task <int> Count(ContractFileGroupingFilter filter)
        {
            IQueryable <ContractFileGroupingDAO> ContractFileGroupings = DataContext.ContractFileGrouping.AsNoTracking();

            ContractFileGroupings = DynamicFilter(ContractFileGroupings, filter);
            return(await ContractFileGroupings.CountAsync());
        }
        public async Task <List <ContractFileGrouping> > List(ContractFileGroupingFilter filter)
        {
            if (filter == null)
            {
                return(new List <ContractFileGrouping>());
            }
            IQueryable <ContractFileGroupingDAO> ContractFileGroupingDAOs = DataContext.ContractFileGrouping.AsNoTracking();

            ContractFileGroupingDAOs = DynamicFilter(ContractFileGroupingDAOs, filter);
            ContractFileGroupingDAOs = DynamicOrder(ContractFileGroupingDAOs, filter);
            List <ContractFileGrouping> ContractFileGroupings = await DynamicSelect(ContractFileGroupingDAOs, filter);

            return(ContractFileGroupings);
        }
        private IQueryable <ContractFileGroupingDAO> OrFilter(IQueryable <ContractFileGroupingDAO> query, ContractFileGroupingFilter filter)
        {
            if (filter.OrFilter == null || filter.OrFilter.Count == 0)
            {
                return(query);
            }
            IQueryable <ContractFileGroupingDAO> initQuery = query.Where(q => false);

            foreach (ContractFileGroupingFilter ContractFileGroupingFilter in filter.OrFilter)
            {
                IQueryable <ContractFileGroupingDAO> queryable = query;
                if (ContractFileGroupingFilter.Id != null && ContractFileGroupingFilter.Id.HasValue)
                {
                    queryable = queryable.Where(q => q.Id, ContractFileGroupingFilter.Id);
                }
                if (ContractFileGroupingFilter.Title != null && ContractFileGroupingFilter.Title.HasValue)
                {
                    queryable = queryable.Where(q => q.Title, ContractFileGroupingFilter.Title);
                }
                if (ContractFileGroupingFilter.Description != null && ContractFileGroupingFilter.Description.HasValue)
                {
                    queryable = queryable.Where(q => q.Description, ContractFileGroupingFilter.Description);
                }
                if (ContractFileGroupingFilter.ContractId != null && ContractFileGroupingFilter.ContractId.HasValue)
                {
                    queryable = queryable.Where(q => q.ContractId, ContractFileGroupingFilter.ContractId);
                }
                if (ContractFileGroupingFilter.CreatorId != null && ContractFileGroupingFilter.CreatorId.HasValue)
                {
                    queryable = queryable.Where(q => q.CreatorId, ContractFileGroupingFilter.CreatorId);
                }
                if (ContractFileGroupingFilter.FileTypeId != null && ContractFileGroupingFilter.FileTypeId.HasValue)
                {
                    queryable = queryable.Where(q => q.FileTypeId, ContractFileGroupingFilter.FileTypeId);
                }
                initQuery = initQuery.Union(queryable);
            }
            return(initQuery);
        }
 private IQueryable <ContractFileGroupingDAO> DynamicFilter(IQueryable <ContractFileGroupingDAO> query, ContractFileGroupingFilter filter)
 {
     if (filter == null)
     {
         return(query.Where(q => false));
     }
     query = query.Where(q => !q.DeletedAt.HasValue);
     if (filter.CreatedAt != null && filter.CreatedAt.HasValue)
     {
         query = query.Where(q => q.CreatedAt, filter.CreatedAt);
     }
     if (filter.UpdatedAt != null && filter.UpdatedAt.HasValue)
     {
         query = query.Where(q => q.UpdatedAt, filter.UpdatedAt);
     }
     if (filter.Id != null && filter.Id.HasValue)
     {
         query = query.Where(q => q.Id, filter.Id);
     }
     if (filter.Title != null && filter.Title.HasValue)
     {
         query = query.Where(q => q.Title, filter.Title);
     }
     if (filter.Description != null && filter.Description.HasValue)
     {
         query = query.Where(q => q.Description, filter.Description);
     }
     if (filter.ContractId != null && filter.ContractId.HasValue)
     {
         query = query.Where(q => q.ContractId, filter.ContractId);
     }
     if (filter.CreatorId != null && filter.CreatorId.HasValue)
     {
         query = query.Where(q => q.CreatorId, filter.CreatorId);
     }
     if (filter.FileTypeId != null && filter.FileTypeId.HasValue)
     {
         query = query.Where(q => q.FileTypeId, filter.FileTypeId);
     }
     query = OrFilter(query, filter);
     return(query);
 }
        private async Task <List <ContractFileGrouping> > DynamicSelect(IQueryable <ContractFileGroupingDAO> query, ContractFileGroupingFilter filter)
        {
            List <ContractFileGrouping> ContractFileGroupings = await query.Select(q => new ContractFileGrouping()
            {
                Id          = filter.Selects.Contains(ContractFileGroupingSelect.Id) ? q.Id : default(long),
                Title       = filter.Selects.Contains(ContractFileGroupingSelect.Title) ? q.Title : default(string),
                Description = filter.Selects.Contains(ContractFileGroupingSelect.Description) ? q.Description : default(string),
                ContractId  = filter.Selects.Contains(ContractFileGroupingSelect.Contract) ? q.ContractId : default(long),
                CreatorId   = filter.Selects.Contains(ContractFileGroupingSelect.Creator) ? q.CreatorId : default(long),
                FileTypeId  = filter.Selects.Contains(ContractFileGroupingSelect.FileType) ? q.FileTypeId : default(long),
                Contract    = filter.Selects.Contains(ContractFileGroupingSelect.Contract) && q.Contract != null ? new Contract
                {
                    Id                        = q.Contract.Id,
                    Code                      = q.Contract.Code,
                    Name                      = q.Contract.Name,
                    TotalValue                = q.Contract.TotalValue,
                    ValidityDate              = q.Contract.ValidityDate,
                    ExpirationDate            = q.Contract.ExpirationDate,
                    DeliveryUnit              = q.Contract.DeliveryUnit,
                    InvoiceAddress            = q.Contract.InvoiceAddress,
                    InvoiceZipCode            = q.Contract.InvoiceZipCode,
                    ReceiveAddress            = q.Contract.ReceiveAddress,
                    ReceiveZipCode            = q.Contract.ReceiveZipCode,
                    TermAndCondition          = q.Contract.TermAndCondition,
                    InvoiceNationId           = q.Contract.InvoiceNationId,
                    InvoiceProvinceId         = q.Contract.InvoiceProvinceId,
                    InvoiceDistrictId         = q.Contract.InvoiceDistrictId,
                    ReceiveNationId           = q.Contract.ReceiveNationId,
                    ReceiveProvinceId         = q.Contract.ReceiveProvinceId,
                    ReceiveDistrictId         = q.Contract.ReceiveDistrictId,
                    ContractTypeId            = q.Contract.ContractTypeId,
                    CompanyId                 = q.Contract.CompanyId,
                    OpportunityId             = q.Contract.OpportunityId,
                    OrganizationId            = q.Contract.OrganizationId,
                    AppUserId                 = q.Contract.AppUserId,
                    ContractStatusId          = q.Contract.ContractStatusId,
                    CreatorId                 = q.Contract.CreatorId,
                    CustomerId                = q.Contract.CustomerId,
                    CurrencyId                = q.Contract.CurrencyId,
                    PaymentStatusId           = q.Contract.PaymentStatusId,
                    SubTotal                  = q.Contract.SubTotal,
                    GeneralDiscountPercentage = q.Contract.GeneralDiscountPercentage,
                    GeneralDiscountAmount     = q.Contract.GeneralDiscountAmount,
                    TotalTaxAmountOther       = q.Contract.TotalTaxAmountOther,
                    TotalTaxAmount            = q.Contract.TotalTaxAmount,
                    Total                     = q.Contract.Total,
                } : null,
                Creator = filter.Selects.Contains(ContractFileGroupingSelect.Creator) && q.Creator != null ? new AppUser
                {
                    Id             = q.Creator.Id,
                    Username       = q.Creator.Username,
                    DisplayName    = q.Creator.DisplayName,
                    Address        = q.Creator.Address,
                    Email          = q.Creator.Email,
                    Phone          = q.Creator.Phone,
                    SexId          = q.Creator.SexId,
                    Birthday       = q.Creator.Birthday,
                    Avatar         = q.Creator.Avatar,
                    Department     = q.Creator.Department,
                    OrganizationId = q.Creator.OrganizationId,
                    Longitude      = q.Creator.Longitude,
                    Latitude       = q.Creator.Latitude,
                    StatusId       = q.Creator.StatusId,
                    RowId          = q.Creator.RowId,
                    Used           = q.Creator.Used,
                } : null,
                FileType = filter.Selects.Contains(ContractFileGroupingSelect.FileType) && q.FileType != null ? new FileType
                {
                    Id   = q.FileType.Id,
                    Code = q.FileType.Code,
                    Name = q.FileType.Name,
                } : null,
            }).ToListAsync();

            return(ContractFileGroupings);
        }
        private IQueryable <ContractFileGroupingDAO> DynamicOrder(IQueryable <ContractFileGroupingDAO> query, ContractFileGroupingFilter filter)
        {
            switch (filter.OrderType)
            {
            case OrderType.ASC:
                switch (filter.OrderBy)
                {
                case ContractFileGroupingOrder.Id:
                    query = query.OrderBy(q => q.Id);
                    break;

                case ContractFileGroupingOrder.Title:
                    query = query.OrderBy(q => q.Title);
                    break;

                case ContractFileGroupingOrder.Description:
                    query = query.OrderBy(q => q.Description);
                    break;

                case ContractFileGroupingOrder.Contract:
                    query = query.OrderBy(q => q.ContractId);
                    break;

                case ContractFileGroupingOrder.Creator:
                    query = query.OrderBy(q => q.CreatorId);
                    break;

                case ContractFileGroupingOrder.FileType:
                    query = query.OrderBy(q => q.FileTypeId);
                    break;
                }
                break;

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

                case ContractFileGroupingOrder.Title:
                    query = query.OrderByDescending(q => q.Title);
                    break;

                case ContractFileGroupingOrder.Description:
                    query = query.OrderByDescending(q => q.Description);
                    break;

                case ContractFileGroupingOrder.Contract:
                    query = query.OrderByDescending(q => q.ContractId);
                    break;

                case ContractFileGroupingOrder.Creator:
                    query = query.OrderByDescending(q => q.CreatorId);
                    break;

                case ContractFileGroupingOrder.FileType:
                    query = query.OrderByDescending(q => q.FileTypeId);
                    break;
                }
                break;
            }
            query = query.Skip(filter.Skip).Take(filter.Take);
            return(query);
        }