Dynamic Order (SQL ORDERBY) in LINQ CompiledQuery

♀尐吖头ヾ 提交于 2019-12-04 19:19:37

I think I found it:

Check out this link. It will point you to the VS2008 code samples which contains a Dynamic Linq Query Library which contains the extension method below. This will allow you to go:

Object.OrderBy("ColumnName");

Here is the extension methods, but you may want the whole library.

public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, string ordering, params object[] values) {
    return (IQueryable<T>)OrderBy((IQueryable)source, ordering, values);
}

public static IQueryable OrderBy(this IQueryable source, string ordering, params object[] values) {
    if (source == null) throw new ArgumentNullException("source");
    if (ordering == null) throw new ArgumentNullException("ordering");
    ParameterExpression[] parameters = new ParameterExpression[] {
        Expression.Parameter(source.ElementType, "") };
    ExpressionParser parser = new ExpressionParser(parameters, ordering, values);
    IEnumerable<DynamicOrdering> orderings = parser.ParseOrdering();
    Expression queryExpr = source.Expression;
    string methodAsc = "OrderBy";
    string methodDesc = "OrderByDescending";
    foreach (DynamicOrdering o in orderings) {
        queryExpr = Expression.Call(
            typeof(Queryable), o.Ascending ? methodAsc : methodDesc,
            new Type[] { source.ElementType, o.Selector.Type },
            queryExpr, Expression.Quote(Expression.Lambda(o.Selector, parameters)));
        methodAsc = "ThenBy";
        methodDesc = "ThenByDescending";
    }
    return source.Provider.CreateQuery(queryExpr);
}

I would do it this way, first all you really need is a way to access the property value by string on an object. You could use reflection, but its slow. So use this helper class approach which is based on the tests of http://stefan.rusek.org/Posts/LINQ-Expressions-as-Fast-Reflection-Invoke/3/

    public static class LINQHelper
    {
        public static IComparable OrderByProperty<TClass>(TClass item, 
                                                          string propertyName)
        {
            var t = Expression.Parameter(typeof(TClass), "t");
            var prop = Expression.Property(t, propertyName);
            var exp = Expression.Lambda(prop, t).Compile();
            return (IComparable)exp.DynamicInvoke(item);
        }

     }

The in your code where you want your order by string of property name, in this example col1, you just do the following.

     var myQuery = from i in Items
                   select i;

     myQuery.OrderBy(i=>LINQHelper.OrderByProperty(i,"col1"));

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!