How to create a dynamic 'contains or LIKE' expression for Linq against an OData service.

c# expression-trees linq odata

Question

I'm attempting to use System to develop a dynamic query tool. Linq.Expressions. Expression (4.0 WPF/C) It uses an OData Service as a test bed.

As long as I keep the requirements to ones that provide alternatives like Equal(..), GreaterThan(..), etc., everything has thus far worked. Since there doesn't seem to be a built-in contain/Like condition, I tried creating one myself. There are currently a few articles available. Zzz-9-Zzz is one that I attempted.

Using the aforementioned answer, the where expression that results is

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

This, although lovely, is incorrect since it cannot be converted into an Odata query.

When I apply the criterion "Equal," the outcome is

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

this prompts the OData inquiry.

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

everything is performing as anticipated.

Do I have the solution implemented incorrectly, or is OData not compatible with it?

The transfer should occur to something like...?$filter=substringof('te', LastName ) eq true

Any ideas on how to resolve this?

Regards

Andreas

PS: I changed the called method's name from "Like" to "MyLike" when I implemented the solution extension in a static class. Additionally, I think that portion is acceptable for the time being since the code used to generate the expressions functions with any of the built-in conditions. If required, I may upload portions of it.

1
5
5/23/2017 11:46:24 AM

Accepted Answer

The "like" operator is presently not supported in any way by OData. Therefore, no matter what you do on the client, there is no way to communicate it in the URL that is created. When using string, the client LINQ provider need to construct the substringof since it is supported. Your filter (Where) expression contains a method.

You may use a C# compiler to produce the expression by doing the following:

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, every IQueryable has an Expression property that stores the expression tree needed to execute the query. The expression tree generated by the compiler may be somewhat altered by certain LINQ providers, but for the most part, it should stay relatively same.

1
8/3/2011 10:02:04 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow