0
votes

I'm an oData newbie and trying to construct the $filter parameter that will behave like a "LIKE" sql statement.

Lets say on the Name textbox the user enters the "ABC", this equates to "$filter=name eq 'ABC'".

  1. User enters "ABC*", code should produce "name startswith 'abc'".
  2. User enters "*ABC", code should produce "name endswith 'abc'".
  3. User enters "A*C", code should produce "name startswith 'a' and name endswith 'c'".
  4. Nice to have if it can also do "A*B*C" or more complex variation, but not a requirement.

Now, I can write a little javascript method that may go like this....(pseudo only) (if first character is == "") ? "blah startswith 'ABC'" : "" + (if last character is == "") ? "blah endswith 'ABC'" : "" ...etc..or may be some regex

But its not elegant I'm not sure this is the right approach, Is there any other way to handle LIKEs with oData. Is it possible to pass "blah eq '*A*B*C*'" and modify the options within the Controller to force the option to generate a "LIKE" ?

Note: Not asking for a actual code, just an idea on what approach may work...

Regards, Mike

1
what if user enters *A*B*C*?Iłya Bursov
yes, i thought about that, but we have to consider the limitations of oData options and LINQ. I would consider "ABC" as an edge case in a real world business application and I might be able to get away with not supporting it.mick_at_oz
can anyone explain why this was down voted to -1 ?mick_at_oz
I suppose because this site is not about "doing your work", but answer your questionsIłya Bursov
good point, i'll modify my questionmick_at_oz

1 Answers

1
votes

OData was "invented" so you won't need to re-invent the wheel...

strings functions

From the Specifications

Now this covers everything your need:

  1. startswith(Name,'ABC')
  2. endswith(Name,'ABC')
  3. startswith(Name,'A') and endswith(Name,'C')