public static string GetDescription(this SortFieldDirection value)
        {
            DescriptionAttribute attribute = value.GetType()
                                             .GetField(value.ToString())
                                             .GetCustomAttributes(typeof(DescriptionAttribute), false)
                                             .SingleOrDefault() as DescriptionAttribute;

            return(attribute == null?value.ToString() : attribute.Description);
        }
Exemple #2
0
        public SortField(
            string field,
            string direction)
        {
            Field = field;

            direction = direction ?? "ASC";
            direction = direction.ToUpper();
            direction = direction == "ASC" || direction == "DESC" ? direction : "ASC";

            Direction = direction == "ASC" ? SortFieldDirection.Ascending
                                           : SortFieldDirection.Descending;
        }
Exemple #3
0
        public static IQueryable <TEntity> ApplyMultipleOrders <TEntity>(
            this IEnumerable <TEntity> DbSet,
            params SortField[] ordersFields)
        {
            bool firstProperty = true;

            IQueryable <TEntity> newQuery = DbSet.AsQueryable();

            foreach (SortField clausulaOrderBy in ordersFields)
            {
                string             propertyOrder     = clausulaOrderBy.Field;
                SortFieldDirection propertyDirection = clausulaOrderBy.Direction;

                //NA PRIMEIRA ORDENAÇÃO USAMOS O OrderBy e OrderByDescending
                if (firstProperty)
                {
                    if (propertyDirection == SortFieldDirection.Ascending)
                    {
                        //USAMOS O MÉTODO ESTENDIDO DE OrderBy
                        newQuery = (IOrderedQueryable <TEntity>)DbSet.OrderBy(propertyOrder);
                    }
                    else
                    {
                        //USAMOS O MÉTODO ESTENDIDO DE OrderByDescending
                        newQuery = (IOrderedQueryable <TEntity>)DbSet.OrderByDescending(propertyOrder);
                    }

                    firstProperty = false;
                }
                //DA SEGUNDA ORDENAÇÃO EM DIANTE USAMOS O ThenBy e ThenByDescending
                else
                {
                    if (propertyDirection == SortFieldDirection.Ascending)
                    {
                        //USAMOS O MÉTODO ESTENDIDO DE OrderBy
                        newQuery = ((IOrderedQueryable <TEntity>)newQuery).ThenBy(propertyOrder).AsQueryable();
                    }
                    else
                    {
                        //USAMOS O MÉTODO ESTENDIDO DE OrderByDescending
                        newQuery = ((IOrderedQueryable <TEntity>)newQuery).ThenByDescending(propertyOrder).AsQueryable();
                    }
                }
            }

            return(newQuery);
        }
Exemple #4
0
 public SortField(string field, SortFieldDirection direction)
 {
     Field     = field;
     Direction = direction;
 }