简体   繁体   English

动态构建lambda表达式

[英]Build lambda expression dynamically

I am trying to sort a object collection dynamically. 我试图动态排序对象集合。 Ideally I would want it to do the below functionality where I could specify the criteria at runtime. 理想情况下,我希望它具有以下功能,可以在运行时指定条件。

_group.OrderByDescending(rec => "rec.CalculatedRecord.GL.PropertyValue").Take(Convert.ToInt16(_filters.Value))

I tried dynamic linq which didn't work. 我尝试了动态linq,但没有用。

var query = (from enumerable in _group
                           orderby "rec.CalculatedRecord.GL.PropertyValue").  descending 
                          select enumerable).Take(5);

The OrderByDescending method takes a parameter of Func<TSource, TKey> . OrderByDescending方法采用Func<TSource, TKey>的参数。 While you can insert a lambda here, as is the most common use case, you can also pass in a variable. 尽管您可以在此处插入lambda(这是最常见的用例),但是您也可以传递变量。 The variable can be dynamically assigned based on whatever you logic might be. 可以根据逻辑逻辑动态分配变量。

I have had to infer your class structure, but here's a basic example: 我不得不推断您的类结构,但这是一个基本示例:

    public class Record
    {
        public CalculatedRecord CalculatedRecord { get; set; }
    }

    public class CalculatedRecord
    {
        public GL GL { get; set; }
    }

    public class GL
    {
        public PropertyValue PropertyValue1 { get; set; }
        public PropertyValue PropertyValue2 { get; set; }
    }

    public class PropertyValue { }

Then you can create the following logic: 然后,您可以创建以下逻辑:

    public class TestHarness
    {
        public void Test()
        {
            IEnumerable<Record> Group = new List<Record>();

            //Simple flag
            bool someCriteria = true;

            //Define orderByFunc
            Func<Record, PropertyValue> orderByFunc;

            //Assign the order by function based on someCriteria 
            if (someCriteria)
            {
                orderByFunc = record => 
                              record.CalculatedRecord.GL.PropertyValue1;
            }
            else
            {
                orderByFunc = record => 
                              record.CalculatedRecord.GL.PropertyValue2;
            }

            //Execute OrderBy
            var updated = Group.OrderByDescending(orderByFunc).Take(5);

        }
    }

However, this doesn't allow you to generate the Func dynamically at runtime. 但是,这不允许您在运行时动态生成Func。 In order to do that you will need to build an Expression using expression trees and then compile it. 为此,您将需要使用表达式树构建一个Expression ,然后对其进行编译。 You can follow the steps at Expression.Lambda and query generation at runtime, simplest "Where" example 您可以按照Expression.Lambda中的步骤进行操作, 并在运行时生成查询,这是最简单的“ Where”示例

A note on why the second one shouldn't work: your object of enumeration is called enumerable yet in your string you are still using rec as in the lambda earlier. 关于第二个为什么不起作用的注释:您的枚举对象被称为enumerable但在您的字符串中您仍像先前的lambda一样使用rec changing the string to use enumerable instead of rec might alleviate your issues. 将字符串更改为使用enumerable而不是rec可以减轻您的问题。

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

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