'Can't ignore case while sorting items in LINQ
I am trying to make a sorting on specific table column, initially I tried to do it dynamically using
baseQuery = baseQuery.OrderBy(x => EF.Property<Table>(x.Field, model.TableColumn).Equals("N/A") ? null : EF.Property<Table>(x.Field, model.TableColumn))
and while it does sort the values in ascending order in this case, it does not sort them while ignoring case. I tried adding StringComparer
baseQuery = baseQuery.OrderBy(x => EF.Property<Table>(x.Field, model.TableColumn).Equals("N/A") ? null : EF.Property<Table>(x.Field, model.TableColumn), StringComparer.OrdinalIgnoreCase);
but it is giving me an error:
The type arguments for method 'Enumerable.OrderBy<TSource,TKey>(IEnumerable,Func<TSource,TKey>,IComparer)'cannot be inferred from the usage. Try specifying the type arguments explicitly.
I tried using a more straightforward sorting
baseQuery = baseQuery.OrderBy(x => x.Field!=null && x.Field.Field1!="N/A"?x.Field.Field1:null, StringComparer.OrdinalIgnoreCase);
But it is telling me the LINQ expression could not be translated.
I even tried using
baseQuery = baseQuery.OrderBy(x => x.Field!=null && x.Field.Field1!="N/A"?x.Field.Field1.ToUpper():null);
but it isn't working.
How can I ignore case while sorting table data in LINQ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
