0

I'm building expression from parameters for filtering and came across a problem, expression looks something like this:

(x => 
    (!specParams.CarModelId.HasValue || x.CarModelId == specParams.CarModelId.Value) &&
    (string.IsNullOrWhiteSpace(specParams.CarModelName) || x.CarModel.Name.NormalizedContains(specParams.CarModelName))
)

problem acquires with NormalizedContains() extension method that I wrote, as it can't be translated as an Expression, the extension method:

public static bool NormalizedContains(this string @string, string value) => 
    @string.ToLower().Contains(value.ToLower()); // may have other manipulations with strings before comparing

How can I rewrite the extension method as an Expression?

Working Pickle
  • 122
  • 3
  • 10
  • Well, the first thing you should do is handwrite at least one query that incorporates the body of `NormalizedContains` to a) confirm that the logic itself is translatable (you mention other manipulations) and b) once you have the query, you can use a tool like LinqPad or even just examine the resulting expression in the debugger to figure out how you need to build out the expression tree. – Kirk Woll May 27 '22 at 21:13
  • Check this my [answer](https://stackoverflow.com/a/66386142/10646316). You can do that with third party extensions. – Svyatoslav Danyliv May 28 '22 at 06:34

1 Answers1

0

Translating Linq Expression to Sql statement depends on EF version that you didn't mentioned.

But, short answer is do not use static method to prevents Unsupported client evaluation error or use AsEnumerable if It's possible for your size of data.

More info on Microsoft Docs : Client vs. Server Evaluation

XAMT
  • 1,515
  • 2
  • 11
  • 31