0

I have the following lambdas generated by reflection and need to know how to condense these into a single expression:

Current:

{((txliferequest.OLifE.Party.Any(party => party.Address.Any(address => address.Line1.Contains("Address 1"))) AndAlso 
   txliferequest.OLifE.Party.Any(party => party.Address.Any(address => address.Line2.Contains("Address 2")))) AndAlso 
   txliferequest.OLifE.Party.Any(party => party.Address.Any(address => address.City.Contains("City"))))}

Needed:

   {((txliferequest.OLifE.Party.Any(party => party.Address.Any(address => {
        address.Line1.Contains("Address 1") AndAlso
        address.Line2.Contains("Address 2") AndAlso
        address.City.Contains("City")
        }) 
   )}

Is there a way to do this using the Expression API? It says the final expression is not able to be reduced.

Chuck D
  • 1,629
  • 2
  • 16
  • 32
  • 1
    Why not flatten the addresses with `SelectMany`? Eg `txliferequest.OLifE.Party.SelectMany(party=>party.Address).Any(address=> address.Line1.Contains("Address 1") && adress.Line2.Contains("Address 2) ...)` ? – Panagiotis Kanavos Jun 29 '18 at 14:51
  • The reflection logic that is building the expressions is not wired to do that so I'm looking to run post processing in certain situations. – Chuck D Jun 29 '18 at 15:24
  • 1
    Perhaps you are looking for a *pre*processing step then, one that simplifies an intermediate expression tree *before* generating `Expression` calls. That's how most compilers and parsers work. Instead of converting the source language directly to the output, they generate an intermediate tree first. – Panagiotis Kanavos Jun 29 '18 at 15:29
  • In this case, something saw that it needs to check nested objects against a list of values. At this point instead of generating the original code, it could *flatten* the objects using `SelectMany` and check the `Address` instances against the list of values. It's a LOT harder to understand what's going on and simplify it by inspecting the end result – Panagiotis Kanavos Jun 29 '18 at 15:32
  • Yes, I will most likely have to do post processing before the final expression is built. Thanks for the insight! – Chuck D Jun 29 '18 at 15:39

0 Answers0