public CommandResult <PaginatedQueryResult <Motorista> > ObterPaginado(int pagina, int quantidade, MotoristaSort sort, bool ascending, string nome)
        {
            var listaBase = ObterBase(sort, ascending, nome);
            var total     = listaBase.Count();
            var skip      = Pagination.PagesToSkip(quantidade, total, pagina);

            var resultado = new PaginatedQueryResult <Motorista>()
            {
                Total = total,
                Data  = listaBase.Skip(skip).Take(quantidade).ToArray()
            };

            return(CommandResult <PaginatedQueryResult <Motorista> > .Valid(resultado));
        }
        private IQueryable <Motorista> ObterBase(MotoristaSort sort, bool ascending, string nome)
        {
            IQueryable <Motorista> query = dataContext.Motorista.AsNoTracking();

            switch (sort)
            {
            case MotoristaSort.Cod_Motorista:
                if (ascending)
                {
                    query = query.OrderBy(a => a.Cod_Motorista == null ? 0 : 1).ThenBy(a => a.Cod_Motorista);
                }
                else
                {
                    query = query.OrderByDescending(a => a.Cod_Motorista == null ? 0 : 1).ThenByDescending(a => a.Cod_Motorista);
                }
                break;

            case MotoristaSort.Ajudante1:
                if (ascending)
                {
                    query = query.OrderBy(a => a.Ajudante1 == null ? 0 : 1).ThenBy(a => a.Ajudante1);
                }
                else
                {
                    query = query.OrderByDescending(a => a.Ajudante1 == null ? 0 : 1).ThenByDescending(a => a.Ajudante1);
                }
                break;

            case MotoristaSort.Ajudante2:
                if (ascending)
                {
                    query = query.OrderBy(a => a.Ajudante2 == null ? 0 : 1).ThenBy(a => a.Ajudante2);
                }
                else
                {
                    query = query.OrderByDescending(a => a.Ajudante2 == null ? 0 : 1).ThenByDescending(a => a.Ajudante2);
                }
                break;

            case MotoristaSort.Telefone1:
                if (ascending)
                {
                    query = query.OrderBy(a => a.Telefone1 == null ? 0 : 1).ThenBy(a => a.Telefone1);
                }
                else
                {
                    query = query.OrderByDescending(a => a.Telefone1 == null ? 0 : 1).ThenByDescending(a => a.Telefone1);
                }
                break;

            case MotoristaSort.Telefone2:
                if (ascending)
                {
                    query = query.OrderBy(a => a.Telefone2 == null ? 0 : 1).ThenBy(a => a.Telefone2);
                }
                else
                {
                    query = query.OrderByDescending(a => a.Telefone2 == null ? 0 : 1).ThenByDescending(a => a.Telefone2);
                }
                break;


            case MotoristaSort.Placa:
                if (ascending)
                {
                    query = query.OrderBy(a => a.Placa == null ? 0 : 1).ThenBy(a => a.Placa);
                }
                else
                {
                    query = query.OrderByDescending(a => a.Placa == null ? 0 : 1).ThenByDescending(a => a.Placa);
                }
                break;


            case MotoristaSort.Nome:
            default:
                query = query.SortBy(x => x.Nome, ascending);
                break;
            }

            return(query.WhereIfNotNull(x => x.Nome.ToUpper().Contains(x.Nome.ToString()), nome));
        }
        public CommandResult <IReadOnlyCollection <Motorista> > Obter(MotoristaSort sort, bool ascending, string nome)
        {
            var resultado = ObterBase(sort, ascending, nome).ToArray();

            return(CommandResult <IReadOnlyCollection <Motorista> > .Valid(resultado));
        }