如何在阵列上使用表达树?

发布于 2025-01-31 15:12:29 字数 870 浏览 2 评论 0原文

我有一个输入字符串,然后我想转换为表达式树,然后我想返回为 func< int,bool> ,我真的不知道它在数组或列表上如何工作。

    //List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    static Func<int, bool> Test(string path, string listOrArray, string input) //its gonna be dynamic type later
    {
        // x => (numbers[x] % 2 != 0) && (numbers[x] % 3 != 0);   <-- I wanna return this
        // x => (numbers[x] % 2 != 0);                            <-- or this

        var type = Expression.Parameter(Type.GetType(path)); 
        var prop = Expression.PropertyOrField(type, listOrArray);

        var a = Expression.Constant(3);
        var b = Expression.Constant(2);
        var c = Expression.Constant(0);

        //var temp = Expression<Func<int, bool>> smth;
        //return temp.Compile();
    }

I've an input string and I want to convert to an expression tree after that I want return as Func<int, bool>, I don't really know how does it work on arrays or lists.

    //List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    static Func<int, bool> Test(string path, string listOrArray, string input) //its gonna be dynamic type later
    {
        // x => (numbers[x] % 2 != 0) && (numbers[x] % 3 != 0);   <-- I wanna return this
        // x => (numbers[x] % 2 != 0);                            <-- or this

        var type = Expression.Parameter(Type.GetType(path)); 
        var prop = Expression.PropertyOrField(type, listOrArray);

        var a = Expression.Constant(3);
        var b = Expression.Constant(2);
        var c = Expression.Constant(0);

        //var temp = Expression<Func<int, bool>> smth;
        //return temp.Compile();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-02-07 15:12:29

我花了一些时间研究您的问题,这是我找到的解决方案,使用numbers.Select():

using System.Linq.Expressions;

List<int> numbers = new List<int>();
for(int i = 0; i < 10; i++)
    numbers.Add(i);

Expression<Func<int, bool>> myfunc = num => num % 2 != 0;
Func<int, bool> myfunc2 = myfunc.Compile();

List<bool> computed = numbers.Select(myfunc2).ToList();

for(int i = 0; i < 10; i++)
{
    Console.Out.WriteLine("Num: " + numbers[i] + " | Computed: " + computed[i]);
}

计算的列表包含每个值

输出的lambda函数的输出:

Num: 0 | Computed: False
Num: 1 | Computed: True
Num: 2 | Computed: False
Num: 3 | Computed: True
Num: 4 | Computed: False
Num: 5 | Computed: True
Num: 6 | Computed: False
Num: 7 | Computed: True
Num: 8 | Computed: False
Num: 9 | Computed: True

表达式树 select(select()source

I spent some time researching your problem, and here is the solution I found, using numbers.Select():

using System.Linq.Expressions;

List<int> numbers = new List<int>();
for(int i = 0; i < 10; i++)
    numbers.Add(i);

Expression<Func<int, bool>> myfunc = num => num % 2 != 0;
Func<int, bool> myfunc2 = myfunc.Compile();

List<bool> computed = numbers.Select(myfunc2).ToList();

for(int i = 0; i < 10; i++)
{
    Console.Out.WriteLine("Num: " + numbers[i] + " | Computed: " + computed[i]);
}

The computed list contains the output of the lambda function for each value

Output:

Num: 0 | Computed: False
Num: 1 | Computed: True
Num: 2 | Computed: False
Num: 3 | Computed: True
Num: 4 | Computed: False
Num: 5 | Computed: True
Num: 6 | Computed: False
Num: 7 | Computed: True
Num: 8 | Computed: False
Num: 9 | Computed: True

Expression Trees Source Select() Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文