Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData Linq to Query String

Tags:

c#

linq

odata

I'm using Web API with OData Query support (nighties) and while I'm aware that the WCF library gives you the ability to query WCF RIA Service URLs what I'm looking for is a lightweight way of generating Odata Query strings sort of like LinqPad does but more generically.

For instance if we know that a service returns a specific type of say "ProductDTO", I want to be able to do something like this:

(from p in ODataSource<ProductDTO>
 where p.Name == "hi"
 select new {p.Model, Name}).ToODataQuery();

Which would return the appropriate $filter and $select commands as a string that can be appended to the URL.

Anyone know of any library that can do something like this?

like image 748
James Hancock Avatar asked Oct 31 '12 19:10

James Hancock


3 Answers

Another possiblity would be to use the Simple.OData.Client as outline here: Can this library be used to generate the request url only?.

like image 124
Dejan Avatar answered Nov 06 '22 06:11

Dejan


You could try this: https://github.com/ubergeoff/HollowPoint.Azure

ODataSource.Query()
.Where(t => t.Age >= 16 && t.Age < 33)
.ToODataString();   

Would output the OData query text:

((Age ge 16) and (Age lt 33))
like image 29
UberGeoff Avatar answered Nov 06 '22 08:11

UberGeoff


I haven't used it, but am researching a similar topic, and this looks like it should provide the functionality that you're looking for: https://bitbucket.org/jjrdk/linq2rest/wiki/Home

Here's an article that shows using it as an oData client: http://blog.petegoo.com/index.php/2012/03/11/creating-a-net-queryable-client-for-asp-net-web-api-odata-services/

Based on the wiki (and not based on any experience with linq2rest), it appears that it goes both ways - LINQ expression to query string, and query string to LINQ expression.

like image 44
crimbo Avatar answered Nov 06 '22 06:11

crimbo