Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:23:25+00:00 2026-05-13T21:23:25+00:00

I have an Expression<Func<T,DateTime>> I want to take the DateTime part of the expression

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:23:25+00:00Added an answer on May 13, 2026 at 9:23 pm

    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;
        }                                        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, if you have an expression like this: Expression<Func<int, int>> fn = x
I have created a repository method with the following signature: T Get<TProperty>(int id, Expression<Func<T,
I have a method to Generate Expression By Clause as below: internal static Expression<Func<TModel,
I have a toolkit that has many methods often taking Expression<Func<T,TProperty>> as parameters. Some
I would have expected delegates generated from expression trees to achieve roughly the same
I have an Expression in the following form: Expression<Func<T, bool>> predicate = t =>
I want to build an expression for a Where clause Expression<Func<T, bool>> on a
I have this generic method in my repository: public T GetFirstOrDefault(Expression<Func<T, bool>> where, Expression<Func<T,
I have this expression : Expression<Func<string, bool>> f = s => s.Length < 5;
I have several Expression<Func<User,bool>> expressions that shares properties. For example, Expression<Func<User, bool>> e1 =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.