如何创建一个动态“中包含或LIKE'表达与LINQ的使用对的OData服务

问题描述:

我try'n创建使用System.Linq.Expressions.Expression(WPF / C#4.0)
它违背OData服务。

I'm try'n to create a dynamic query tool using System.Linq.Expressions.Expression (WPF/c#4.0) It runs against an OData Service.

到目前为止,一切工作只要我限制打造比如等于选项(..)的条件,GREATERTHAN(..)等
似乎有在包含没有构建/像条件,所以我尝试建设自己。有文章了一把已经在那里。一个我想是 HOWTO创建System.Linq.Expressions.Expression对于像

So far, all is working as long as I limit the conditions to build in options like Equal(..), GreaterThan(..) etc. There seems to be no build in contains/Like condition so I tried building my own. There are a handful of articles already out there. One I tried is Howto create a System.Linq.Expressions.Expression for Like.

现在,如果我使用上述溶液中,所产生的,其中表达

Now if I use the above solution, the resulting where expression is

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

这是很好的,但错了,因为它并没有转化成有效的OData查询。

which is nice, but wrong, since it does not translate into a valid Odata query.

如果我使用条件'平等',结果是

If I use condition 'Equal', the result is

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

而导致的OData查询

which results in the OData query

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

和按预期工作。

我做得不对的解决方案的实施,或者可以将它与OData的使用?

Am I doing something wrong with the implementation of the solution, or can it not be used with OData?

应该转移到像 ...?$ =过滤substringof('德',姓氏)EQ真

如何解决这一问题?

问候

安德烈亚斯

PS,我实现了一个静态类解决方案的扩展,我所改变的是调用方法的名字从'像'到'MyLike '
此外,由于用于构建表达式的代码工作与任何内置的条件下,我想,现在这部分是确定。如果需要的话我可以发布它的部分。

PS, I implemented the solution extension in a static class, all I changed is the name of the called method from 'Like' to 'MyLike' Also, since the code used to build the expressions is working with any of the build-in condition, I assume, for now that part is ok. I can post parts of it if needed

的OData目前不支持喜欢操作符的。所以,无论你在客户端上做什么,产生的URL没有表达的方式。
的substringof支持,当您使用string.Contains方法在你的过滤器(凡)表达客户LINQ提供程序应生成它。

OData currently doesn't support the "like" operator at all. So no matter what you do on the client, the URL produced has not way of expressing that. The substringof is supported and the client LINQ provider should generate it when you use string.Contains method in your filter (Where) expression.

要得到表达式由C#编译器,你可以做这样的事情发生:

To get the expression generated by a 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());



基本上任何IQueryable的具有包含查询运行表达式树的属性表达。一些LINQ提供商可能由编译器生成原来有所变化表达式树,但最应该离开它接近原始。

Basically any IQueryable has a property Expression which contains the expression tree for the query to run. Some LINQ providers might change the expression tree somewhat from the original one created by the compiler, but most should leave it close to the original.