Dynamic linq in .net core


#1

hi,
I’m trying to implement dynamic linq in .net core
I was using this in 4.5 : http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
basically I want to be able to do something like this:

queryable.OrderBy("Name");

this is what I have so far:

public static class Dlinq
{
    public static IQueryable<T> OrderBy<T>(IQueryable<T> source, string ordering)
    {
        var type = typeof(T);
        var property = type.GetProperty(ordering);
        var parameter = Expression.Parameter(type, "p");
        var propertyAccess = Expression.MakeMemberAccess(parameter, property);
        var orderByExp = Expression.Lambda(propertyAccess, parameter);
        MethodCallExpression resultExp = Expression.Call(typeof(IQueryable), "OrderBy", new Type[] { type, property.PropertyType }, source.Expression, Expression.Quote(orderByExp));
        return source.Provider.CreateQuery<T>(resultExp);
    }
}

it’s a method similar to what I had in 4.5,
my problems are that

  1. there is no Queryable type in .net core, I used Expression.Call(typeof(IQueryable)
    and I think that this probably won’t work because it’s an interface
  2. there is no .AsQueryabel() in .net core so I can’t test/use this method
    in 4.5 I would have an array Foo[] and call foos.AsQueryable.OrderBy(“Name”)

#2

problem solved
I had to add nuget package System.Linq.Queryable


.NET Foundation Website | Blog | Projects | Code of Conduct