2

I have two entities (ChildA and ChildB) inheriting from the same entity (Parent). So two expression each from child entities are as follows:

Expression<Func<T, Parent>> aExpression = t=> t.ChildAs.OrderByDescending(o => o.CreatedDate).FirstOrDefault();

Expression<Func<T, Parent>> bExpression = t => t.ChildBs.OrderByDescending(o => o.CreatedDate).FirstOrDefault();

CreatedDate is a field in Parent entity. I want to union these two expressions and put conditions on the final expression. How to union these?

Jitendra Gupta
  • 764
  • 1
  • 8
  • 16
  • Possible duplicate of: http://stackoverflow.com/questions/457316/combining-two-expressions-expressionfunct-bool?rq=1 – dotNET Sep 18 '13 at 08:13
  • Could you post Parent and ChildA/B classes ? – Toto Sep 18 '13 at 08:13
  • Please clarify what do you mean by "union-ing" of expressions ? Do you want a resulting expression which holds some laws ? Is the "union-ing" of the two expressions still in the form of `Expression>` ? – Eduard Dumitru Sep 18 '13 at 08:16
  • @EduardDumitru: Yes, Actually in the child tables records are saved with same IDs in the parent. But for same t there exists records in ChildA and ChildB. But there is no reference for t in Parent table. I want to take latest record of Parent for t. Hence trying to union the latest record from both the tables and getting the latest out of them. – Jitendra Gupta Sep 18 '13 at 08:24
  • 1
    It is not clear where and when you use these expressions in your code. We have no leverage. – Gert Arnold Sep 18 '13 at 08:45

1 Answers1

0

As my requirement was to get the latest Parent of t, I could not find a way to do it through union. But I could do it this way,

Expression<Func<T, Parent>> latestParentExpression = t => (aExpression.Invoke(t).CreatedDate >= bExpression.Invoke(t).CreatedDate)?aExpression.Invoke(t):bExpression.Invoke(t);

Jitendra Gupta
  • 764
  • 1
  • 8
  • 16