static IQueryable <TEntity> BuildOrderByThenBy <TEntity>(this IQueryable <TEntity> query, SearchySort searchySort, Type type, bool mainOrderBy)
        {
            switch (true)
            {
            case object _ when type.Equals(typeof(int)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <int, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <int, TEntity>(searchySort));
                }

            case object _ when type.Equals(typeof(string)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <string, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <string, TEntity>(searchySort));
                }

            case object _ when type.Equals(typeof(DateTime?)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <DateTime?, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <DateTime?, TEntity>(searchySort));
                }

            case object _ when type.Equals(typeof(DateTime)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <DateTime, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <DateTime, TEntity>(searchySort));
                }

            case object _ when type.Equals(typeof(byte)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <byte, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <byte, TEntity>(searchySort));
                }

            case object _ when type.Equals(typeof(short)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <short, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <short, TEntity>(searchySort));
                }

            case object _ when type.Equals(typeof(decimal)):

                if (mainOrderBy)
                {
                    return(query.BuildOrderBy <decimal, TEntity>(searchySort));
                }
                else
                {
                    return(query.BuildThenBy <decimal, TEntity>(searchySort));
                }

            default:

                throw new SWException("Unsupported sort datatype: " + type.ToString());
            }
        }
        static IQueryable <TEntity> BuildThenBy <U, TEntity>(this IQueryable <TEntity> query, SearchySort searchySort)
        {
            var pe           = Expression.Parameter(typeof(TEntity), "");
            var orderedQuery = (IOrderedQueryable <TEntity>)query;

            if (searchySort.Sort == SearchySortOrder.ASC)
            {
                return(orderedQuery.ThenBy(Expression.Lambda <Func <TEntity, U> >(Expression.Property(pe, searchySort.Field), new ParameterExpression[] { pe })));
            }
            else
            {
                return(orderedQuery.ThenByDescending(Expression.Lambda <Func <TEntity, U> >(Expression.Property(pe, searchySort.Field), new ParameterExpression[] { pe })));
            }
        }