0

I have a method like:

public Expression<Func<Person, bool>> FilterPeople(string searchCriteria)
{
    return (person => 
        person.Address.Line1.Contains(searchCriteria) ||
        person.Address.Line2.Contains(searchCriteria) ||
        person.Address.Line3.Contains(searchCriteria));
}

This method is used in conjunction with Entity Framework to perform a Where() filter on the results (the real code as a few more conditions than this example).

If I wanted to shorten person.Address to something more manageable, like a, I can't figure out how I would do that.

I thought something like Ref Locals would do the trick, but I can't work out how to assign that inside the expression body.

Any ideas?

aaron-bond
  • 3,101
  • 3
  • 24
  • 41
  • 1
    For a situation this simple any solution would be way more complicated and be less readable or usable than what you already have. If the operation you were trying to separate out was much longer and more complex, then it could potentially actually be worth doing. – Servy Jul 02 '21 at 15:54
  • @Servy - the real world example has much longer object trees and many more conditions. That being said, the above obviously works - this is moreso a learning exercise. If you know of any documentation that explains the "complicated" way that would do the trick also. – aaron-bond Jul 02 '21 at 15:56
  • 1
    There is no easy way. It is possible but needs expression transformations. Bad idea to complicate things because of such minor readable improvements. – Svyatoslav Danyliv Jul 02 '21 at 15:58
  • Good to know, thanks! – aaron-bond Jul 02 '21 at 16:00
  • @SvyatoslavDanyliv I'd say the way to do it is pretty easy. In this specific situation the existing code is so easy it's just faster to type the code in the question than a solution, but if the actual code really is more complicated in the operation to factor out then the solution is useful enough. Although it's most useful when the operation to factor out isn't constant, as you can't "hard code" it in that case. – Servy Jul 02 '21 at 16:01
  • @Servy, well I don't know easy way even after years of writhing LINQ translator. But probably I have missed something. – Svyatoslav Danyliv Jul 02 '21 at 16:04
  • @SvyatoslavDanyliv The solution is in the duplicate. From the caller's perspective it's just one method call, the implementation is also just a few lines of code. Compared to most expression transformations, which certainly can end up rather complex, this is pretty straightforward. – Servy Jul 02 '21 at 16:06
  • @Servy, well you just confirmed my thoughts - expression tree transformations are needed. – Svyatoslav Danyliv Jul 02 '21 at 16:08
  • @SvyatoslavDanyliv Sure, the only way to avoid transforming the expression tree is to hard code the expression you want in the final result. But there are simple transformations and complex transformations. This one is about as simple as it gets. – Servy Jul 02 '21 at 16:10
  • @Servy, for me it is also simple transformations, but here I do not see any benefits. Anyway thanks for small discussion ;) – Svyatoslav Danyliv Jul 02 '21 at 16:19
  • @SvyatoslavDanyliv Sure, but the OP specifically said that the real world example is much more complicated than this example. Hence why I didn't add the duplicate until after they said as much. – Servy Jul 02 '21 at 16:21

0 Answers0