可重用的LINQ方法查询

时间:2016-12-27 06:20:19

标签: c# linq expression-trees dynamic-linq

是否可以通过调用LINQOrderBy来创建可重复使用的Where查询表达式,而无需将其应用于特定的IQueryable

我希望能够做到这样的事情:

var data = new List<PlayerDTO>();
var builder = new Builder<PlayerDTO>();
var query = builder.GetQuery();
var results = data.AsQueryable().Provider.CreateQuery(query);

如果没有将IQueryable对象提供给GetQuery方法,我就无法解决此问题。

这是我到目前为止的代码:

public Expression GetQuery()
{
    var type = typeof(T); // T is PlayerDTO
    var col = type.GetProperty(OrderBy.Column);

    var orderByMethod = typeof(Queryable).GetMethods().Single(
        method => method.Name == "OrderBy"
                  && method.IsGenericMethodDefinition
                  && method.GetGenericArguments().Length == 2
                  && method.GetParameters().Length == 2);

    var genericOrdebyMethod = orderByMethod.MakeGenericMethod(typeof(T), col.PropertyType);

    var parameter = Expression.Parameter(typeof(T), "p"); // {p}
    var property = Expression.Property(parameter, col); // {p.ID}
    var lambda = Expression.Lambda<Func<T, int>>(property, parameter); // {p => p.ID}

    //This list should not exist here
    var tempList = new List<PlayerDTO>() { new PlayerDTO(1, "First"), new PlayerDTO(2, "Second") };

    var orderByMethodExpression = Expression.Call(genericOrdebyMethod, tempList.AsQueryable().Expression, lambda); // {tempList.OrderBy(p => p.ID)}

    var results = tempList.AsQueryable().Provider.CreateQuery(orderByMethodExpression);

    return orderByMethodExpression;
}

相关部分是对Expression.Call的调用,我必须在其中提供IQueryable以便它可以工作,但我希望能够构建表达式而不指定现有的{{1} }}

这甚至可能吗?

修改
现在我想到这一点,我实际上根本不需要这样做......将IQueryable作为参数发送到GetQuery方法是完全合理的。我会保留这个问题。

1 个答案:

答案 0 :(得分:1)

您可以将其创建为从IQueryable到IOrderedQueryable的另一个表达式,如:

public Expression getOrderByQuery<T>(PropertyInfo col)
{
    var orderByMethod = typeof(Queryable).GetMethods().Single(
        method => method.Name == "OrderBy"
                  && method.IsGenericMethodDefinition
                  && method.GetGenericArguments().Length == 2
                  && method.GetParameters().Length == 2);

    var genericOrdebyMethod = 
                        orderByMethod.MakeGenericMethod(typeof(T), col.PropertyType);

    var parameter = Expression.Parameter(typeof(T), "p"); // {p}
    var property = Expression.Property(parameter, col); // {p.ID}
    var lambda = Expression.Lambda<Func<T, int>>(property, parameter); // {p => p.ID}

    var paramList = Expression.Parameter(typeof(IQueryable<T>));

    // {tempList.OrderBy(p => p.ID)}    
    var orderByMethodExpression = Expression.Call(genericOrdebyMethod, paramList, lambda); 

    return Expression.Lambda(orderByMethodExpression, paramList);
}

我以这种方式使用它,我的列表被命令:

var tempList = new List<PlayerDTO>() 
  { 
     new PlayerDTO(2, "First"), 
     new PlayerDTO(1, "Second") 
  };

var e = (Expression<Func<IQueryable<PlayerDTO>, IOrderedQueryable<PlayerDTO>>>)
               getOrderByQuery<PlayerDTO>(typeof(PlayerDTO).GetProperty("Id"));
e.Compile().Invoke(tempList.AsQueryable()).Dump();