How to create a dynamic expression "contains or LIKE" that will be used with Linq for the OData service

I am trying to create a dynamic query tool using System.Linq.Expressions.Expression (WPF / C # 4.0) It works with OData Service.

While everything works, while I limit the conditions for creating options such as Equal (..), GreaterThan (..), etc. There seems to be no assembly in condition / As condition, so I tried to build my own. There are already several articles. One of my attempts - How to create a System.Linq.Expressions.Expression file for Like? .

Now, if I use the above solution, we get the expression where

whereCallExpression = {Convert([10000]).Expand("A,B").Where(clt => MyLike(clt.LastName, "te"))}' 

which is good, but incorrect, as it does not translate into a valid Odata request.

If I use the Equals condition, the result

 whereCallExpression = {Convert([10000]).Expand("A,B").Where(clt => (clt.LastName == "te"))} 

which leads to an OData query p>

 results = {http://.../Clients()?$filter=LastName eq 'te'&$expand=A,B} 

and works as expected.

Am I doing something wrong with implementing the solution or can't use it with OData?

It should go to something like ...?$filter=substringof('te', LastName ) eq true

Any decision on how to fix this?

Hello

Andreas

PS, I implemented a solution extension in a static class, all I changed was the name of the method being called from “Like” to “MyLike”. In addition, since the code used to build the expressions works with any of the build conditions, I assume that now this part is in order. I can send parts of it if necessary

+4
source share
1 answer

OData does not currently support a like operator. So no matter what you do on the client, the generated URL has no way to express this. The substring is supported, and the LINQ client provider must generate it when you use the string.Contains method in your filter (Where) expression.

To get the expression generated by the C # compiler, you can do something like this:

 IQueryable<string> source = new List<string>().AsQueryable(); IQueryable<string> result = from item in source where item.Contains("John") select item; Console.WriteLine(result.Expression.ToString()); 

Basically, any IQueryable has an Expression property that contains an expression tree to run a query. Some LINQ providers may slightly modify the expression tree from the source created by the compiler, but most of them should leave it close to the original.

+1
source

All Articles