简体   繁体   English

动态订单通过使用linq dynamic

[英]dynamic orderBy using linq dynamic

I am trying to convert this Func to use string values using linq.dynamic. 我正在尝试使用linq.dynamic将此Func转换为使用字符串值。

Currently I have 目前我有

Func<IQueryable<Customer>, IOrderedQueryable<Customer>> orderBy = o => o.OrderBy(c => c.Postcode);

But I want to do 但是我想做

string sortItem = "customer";
string order = "ASC"
Func<IQueryable<Customer>, IOrderedQueryable<Customer>> orderBy = o => o.OrderBy(sortItem + " " + order);

I am using the Linq.Dynamic library but I am unable to get it to work with the function. 我正在使用Linq.Dynamic库,但无法使其与该函数一起使用。

Any help... 任何帮助...

Like the other answer suggests, this may not be possible. 就像其他答案所暗示的那样,这可能是不可能的。 However, I wanted to post some code which I wrote recently to do something similar: 但是,我想发布一些我最近编写的代码以执行类似的操作:

// single column sorting support
var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
Func<LegalComplianceDatatable, string> orderingFunction = (c => sortColumnIndex == 0 ? c.HomeCountry :
                                                    sortColumnIndex == 1 ? c.HostCountry :
                                                    sortColumnIndex == 2 ? c.YearOneRate :
                                                    sortColumnIndex == 3 ? c.YearOtherRate :
                                                    sortColumnIndex == 4 ? c.RateType :
                                                    c.HostCountry);

if (Request["sSortDir_0"] == "desc")
{
    filteredResults = filteredResults.OrderByDescending(orderingFunction);
}
else
{
    filteredResults = filteredResults.OrderBy(orderingFunction);
}

I haven't used Linq.Dynamic but you can achieve this if you are comfortable building your own expression tree. 我没有使用过Linq.Dynamic,但是如果您愿意构建自己的表达式树,则可以实现。 For example: 例如:

public static class IQueryableExtension
{
    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> query, string propertyName)
    {

        var memberProp = typeof(T).GetProperty(propertyName);
        var method = typeof(IQueryableExtension).GetMethod("OrderByInternal")
                                   .MakeGenericMethod(typeof(T), memberProp.PropertyType);

        return (IOrderedQueryable<T>)method.Invoke(null, new object[] { query, memberProp });
    }

    public static IOrderedQueryable<T> OrderByInternal<T, TProp>(IQueryable<T> query, PropertyInfo memberProperty)
    {
        if (memberProperty.PropertyType != typeof(TProp)) throw new Exception();

        var thisArg = Expression.Parameter(typeof(T));
        var lamba = Expression.Lambda<Func<T, TProp>>(Expression.Property(thisArg, memberProperty), thisArg);

        return query.OrderBy(lamba);

    }
}

And you can use this like: 您可以像这样使用:

IQueryable<Customer> query; // Some query
query = query.OrderBy("Name"); // Will return an IOrderedQueryable<Customer>

This is the logic for ascending sort, without checking (you will need to make sure the property exists, and so on) and some things can be optimized (the reflected method invocation for example). 这是升序排序的逻辑,无需检查(您需要确保属性存在,等等),并且可以优化某些内容(例如,反映方法调用)。 It should get you started. 它应该让您开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM