Validating C # IQueryable in OrderBy for null ckeck

IQueryable throw "The specified argument is out of range.

the code:

public IQueryable<T> ApplyOrder(IQueryable<T> items, GridSortDirection direction)
        {
            switch (direction)
            {
                case GridSortDirection.Ascending:
                    return items.OrderBy(_expression);
                case GridSortDirection.Descending:
                    return items.OrderByDescending(_expression);
            }
        }

where _expression is a type:

Expression<Func<T, TKey>> expression

and this

{x => x.customers.studentschoolenrolments.ElementAt(0).schools.Name}

I do not want this release. Is it possible to check if an element exists and not return an empty string?

I am trying to use a method like:

public static bool IsNullOrEmpty<T>(this IEnumerable<T> items) {
    return items == null || !items.Any();
}

but I have no idea how to use it in items.OrderBy(_expression);

+4
source share
1 answer

My head loves to mix IEnumerableand IQueryabletherefore it can be pointless (but I don't see any db tags). You cannot use FirstOrDefaultas follows:

{x => x.customers.studentschoolenrolments.FirstOrDefault()?.schools.Name ?? string.Empty}
+2
source

Source: https://habr.com/ru/post/1628624/


All Articles