简体   繁体   中英

Linq2Rest doesn't work on nested properties?

I have an API that returns c# objects serialized as JSON.

I need to implement some sort of OData format parsing on my API URLs. That for example, originally look like this: http://api.loc/userprofiles/all/?$orderby=ProfileProperties.Email

I was looking for some more or less straightforward library that would allow me to parse the Filter / OrderBy (OData) query string and apply a Filter on my data source. I found Linq2Rest with it's pretty simple format where I can use it like:

var filtered = allItems.Filter(Request.Params).OfType<Content.UserProfile>();

Problem is that my Content.UserProfile has a nested property .ProfileProperties, which contains the properties that I'm interested in

So, for example:

var user = new Content.UserProfile();
user.ProfileProperties.Email = "my@mail.com";
user.ProfileProperties.FirstName = "Fukoka";

.. etc

Unfortunately Linq2Rest can't parse my query and fails with a message "ProfileProperties.Email is not recognized as a valid property" ..

How can I then perform this Filter on items with nested properties ?

Is the query URL a generated one, or did you write it yourself? I would have expected it to be:

http://api.loc/userprofiles/all/?$orderby=ProfileProperties/Email

Normally with OData nested properties use a slash ( / ) as separator.

Otherwise, you should include some information about how the query URL is generated.

You can use OData Client Library provided by Microsoft.

Is there any specific reason you are not using it ?

Example taken from Query Options

// Define a query for orders with a Freight value greater than 30 // and that is ordered by the ship date, descending.

  DataServiceQuery<Order> selectedOrders = context.Orders
    .AddQueryOption("$filter", "Freight gt 30")
    .AddQueryOption("$orderby", "OrderID desc");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM