3

I have a class:

public class Person {
        public virtual string Id { get; set; }
        public virtual string Name { get; set; }
        public virtual int Age { get; set; }
        public virtual DateTime UpdateTime { get; set; }
}

And I have two Expression<Func<T, object>> objects:

Expression<Func<Person, object>> exp1 = t => t.UpdateTime
Expression<Func<Person, object>> exp2 = t => new {t.Age, t.Name}

And now I need to merge them to

Expression<Func<Person, object>> exp1 = t => new {t.UpdateTime, t.Age, t.Name}

Is this possible? If so, how?

7
  • 4
    @Thenighthunter in the question you point out, the accepted answer suggests to use AndAlso or OrElse because the predicate returns bool. OP is asking how to merge two objects. So how is this relevant here? Commented Apr 30, 2021 at 10:54
  • 1
    This question needs more details. While it is something that is definitely possible, T in the example is not valid (or rather does not contain enough information) since an arbitrary T can't possibly have the specified properties. Can you show perhaps a full method or class declaration? Is T constrained to something or was it perhaps a bad example and T is really meant to represent a specific claas? Can you show that definition? Commented Apr 30, 2021 at 11:03
  • 1
    In the general case, your problem is that you are creating a new class for the anonymous result, and that is difficult at runtime (and only worthwhile in very specific scenarios). What do you expect to be able to do with the return from the combined Expression?
    – NetMage
    Commented Apr 30, 2021 at 14:44
  • @pinkfloydx33 I've added more details in problem
    – Jonny.ZCH
    Commented May 6, 2021 at 1:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.