0

My project is having some user defined linq extension methods for orderby, ThenBy etc. And I want to sort a list of object based on a string property in that object by ignoring its case.

Code:

public static IOrderedQueryable<TEntity> OrderUsingSortExpression<TEntity>(this IQueryable<TEntity> source, string sortExpression) where TEntity : class
        {
            OrderedQueryable<TEntity> result = null;
            try
            {
                  result =  source.OrderBy("Title") 
                    // I'm getting an exception here in result.

            }
            catch (Exception ex)
            {
               result = source as IOrderedQueryable<TEntity>;
            }
            return result;
        }

    public static IOrderedQueryable<TEntity> OrderBy<TEntity>(this IQueryable<TEntity> source, string fieldName) where TEntity : class


     {
            MethodCallExpression resultExp = GenerateMethodCall<TEntity>(source, "OrderBy", fieldName);
            return source.Provider.CreateQuery<TEntity>(resultExp) as IOrderedQueryable<TEntity>;
        }
    private static MethodCallExpression GenerateMethodCall<TEntity>(IQueryable<TEntity> source, string methodName, String fieldName) where TEntity : class
            {
                Type type = typeof(TEntity);
                Type selectorResultType;
                var StrComp = StringComparer.OrdinalIgnoreCase;
                LambdaExpression selector = GenerateSelector<TEntity>(fieldName, out selectorResultType);
                MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName,
                                new Type[] { type, selectorResultType },
                                source.Expression, Expression.Quote(selector), Expression.Constant(StrComp));
                return resultExp;
            }

But at the end I'm receiving an exception:

Static members = error CS0119: 'NotSupportedException' is a type, which is not valid in the given context

I think, it is expecting Enumerable source instead of Queryable. But I can't change source type. How can I resolve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Dreamer
  • 586
  • 3
  • 7
  • 23
  • 2
    `Linq to SQL` translate your `linq` queries to `sql` the comparison type between strings depends entirely on your database coalition settings. – Alessandro D'Andria Jul 19 '18 at 08:49
  • 2
    Both entity framework (full and core) and linq-to-sql don't support setting the collation (how strings are compared) through `StringComparer` and similar methods. – xanatos Jul 19 '18 at 09:01
  • Note that on SQL Server normally columns are searched in a case insensitive way (see https://stackoverflow.com/questions/1831105/how-to-do-a-case-sensitive-search-in-where-clause-im-using-sql-server) – xanatos Jul 19 '18 at 09:02
  • Could you provide a [mcve] including a call that shows that error? – nvoigt Jul 19 '18 at 10:02
  • @nvoigt I have code in question, please check. – Dreamer Jul 19 '18 at 12:55
  • What does GenerateSelector do? Can you build a ConsoleApplication1 with that code to ensure it's actually a [mcve]? – nvoigt Jul 19 '18 at 13:06
  • It will give you a lambda expression ex. if your fieldname is **"Title"** , the lambda expression will be like, **entity => entity.Title** – Dreamer Jul 19 '18 at 13:13

0 Answers0