从现有表达式创建新表达式

发布于 2024-08-23 13:43:01 字数 783 浏览 4 评论 0 原文

我有一个 Expression> 我想获取表达式的 DateTime 部分并从中提取月份。所以我会把它变成一个 Expression> 我不太确定如何做到这一点。我查看了 ExpressionTree Visitor 但我无法让它工作就像我需要的那样。这是日期时间表达式的示例

DateTimeExpression http://img442.imageshack.us/img442/ 6545/datetimeexpression.png

这是我想要创建的示例 MonthExpression http://img203.imageshack.us/img203/8013/datetimemonthexpression.png

看起来我需要创建一个新的 MemberExpression,它由 DateTime 表达式中的 Month 属性组成,但我不确定。

I have an Expression<Func<T,DateTime>> I want to take the DateTime part of the expression and pull the Month off of it. So I would be turning it into a Expression<Func<T,int>> I'm not really sure how to do this. I looked at the ExpressionTree Visitor but I can't get it to work like I need. Here is an example of the DateTime Expression

DateTimeExpression http://img442.imageshack.us/img442/6545/datetimeexpression.png

Here is an example of what I want to create
MonthExpression http://img203.imageshack.us/img203/8013/datetimemonthexpression.png

It looks like I need to create a new MemberExpression that is made up of the Month property from the DateTime expression but I'm not sure.

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

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

发布评论

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

评论(1

泅渡 2024-08-30 13:43:01

是的,这正是您想要的 - 并使用 < code>Expression.Property 是最简单的方法:

Expression func = Expression.Property(existingFunc.Body, "Month");
Expression<Func<T, int>> lambda = 
    Expression.Lambda<Func<T, int>>(func, existingFunc.Parameters);

我相信应该没问题。它在这个简单的测试中有效:

using System;
using System.Linq.Expressions;

class Person
{
    public DateTime Birthday { get; set; }
}

class Test
{
    static void Main()
    {
        Person jon = new Person 
        { 
            Birthday = new DateTime(1976, 6, 19)
        };

        Expression<Func<Person,DateTime>> dateTimeExtract = p => p.Birthday;
        var monthExtract = ExtractMonth(dateTimeExtract);
        var compiled = monthExtract.Compile();
        Console.WriteLine(compiled(jon));
    }

    static Expression<Func<T,int>> ExtractMonth<T>
        (Expression<Func<T,DateTime>> existingFunc)
    {
        Expression func = Expression.Property(existingFunc.Body, "Month");
        Expression<Func<T, int>> lambda = 
            Expression.Lambda<Func<T, int>>(func, existingFunc.Parameters);
        return lambda;
    }                                        
}

Yes, that's exactly what you want - and using Expression.Property is the easiest way to do that:

Expression func = Expression.Property(existingFunc.Body, "Month");
Expression<Func<T, int>> lambda = 
    Expression.Lambda<Func<T, int>>(func, existingFunc.Parameters);

I believe that should be okay. It works in this simple test:

using System;
using System.Linq.Expressions;

class Person
{
    public DateTime Birthday { get; set; }
}

class Test
{
    static void Main()
    {
        Person jon = new Person 
        { 
            Birthday = new DateTime(1976, 6, 19)
        };

        Expression<Func<Person,DateTime>> dateTimeExtract = p => p.Birthday;
        var monthExtract = ExtractMonth(dateTimeExtract);
        var compiled = monthExtract.Compile();
        Console.WriteLine(compiled(jon));
    }

    static Expression<Func<T,int>> ExtractMonth<T>
        (Expression<Func<T,DateTime>> existingFunc)
    {
        Expression func = Expression.Property(existingFunc.Body, "Month");
        Expression<Func<T, int>> lambda = 
            Expression.Lambda<Func<T, int>>(func, existingFunc.Parameters);
        return lambda;
    }                                        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文