构建动态Lambda表达式时,order和select有什么区别?

时间:2018-08-20 01:40:51

标签: c# dynamic lambda

此类使用字符串参数对记录进行排序。有效

public class Order
{
    public static IEnumerable<T> OrderByDynamic<T>(IEnumerable<T> items, string sortby)
    {
        var property = typeof(T).GetProperty(sortby);

        var result = typeof(Order)
            .GetMethod("OrderByDynamic_Private", BindingFlags.NonPublic | BindingFlags.Static)
            .MakeGenericMethod(typeof(T), property.PropertyType)
            .Invoke(null, new object[] { items, sortby});

        return (IEnumerable<T>)result;
    }

    private static IEnumerable<T> OrderByDynamic_Private<T, TKey>(IEnumerable<T> items, string sortby)
    {
        var parameter = Expression.Parameter(typeof(T), "x");

        Expression<Func<T, TKey>> property_access_expression =
            Expression.Lambda<Func<T, TKey>>(
            Expression.Property(parameter, sortby),
            parameter);
            return items.OrderByDescending(property_access_expression.Compile());
        throw new Exception("Invalid Sort Direction");
    }
}

然后我要使用字符串参数来确定选择的lambda表达式中的列。 只需将所有名称“ order”替换为“ select”,然后将返回值替换为 items.Select(property_access_expression.Compile());

然后我得到了错误“ 错误CS0266无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换” 如何解决此错误。

1 个答案:

答案 0 :(得分:0)

据我了解,您想动态选择字段。我建议您使用不同的方法进行订购和选择。这是如何动态选择给定字段的方法。它仅选择给定的字段,并为其他字段打印默认值。因此,它适用于您的用例。

class test
{
    public string Code { get; set; }
    public int Id { get; set; }

    public override string ToString()
    {
        return Code + " " + Id;
    }
}

class MainClass
{
    public static void Main(string[] args)
    {
        List<test> list = new List<test>();

        list.Add(new test { Code = "1", Id = 5 });
        list.Add(new test { Code = "2", Id = 6 });
        list.Add(new test { Code = "3", Id = 7 });
        list.Add(new test { Code = "4", Id = 8 });

        list.AsQueryable()
             .Select(DynamicSelect<test>("Id"))
             .ToList()
             .ForEach(Console.WriteLine);
    }
}


public static Expression<Func<T, T>> DynamicSelect<T>(string fields)
{
    var members = fields.Split(',').Select(f => f.Trim());
    var targetType = typeof(T);
    var parameter = Expression.Parameter(targetType, "x");
    var bindings = new List<MemberBinding>();
    var target = Expression.Constant(null, targetType);

    foreach (var name in members)
    {
        var targetMember = Expression.PropertyOrField(target, name);
        var sourceMember = Expression.PropertyOrField(parameter, name);
        bindings.Add(Expression.Bind(targetMember.Member, sourceMember));
    }

    var body = Expression.MemberInit(Expression.New(targetType), bindings);
    return Expression.Lambda<Func<T, T>>(body, parameter);
}