AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

Filtering support for nested properties in JSON strings

Open talalekk opened this issue 4 years ago • 2 comments
trafficstars

I am trying to use the OData lib for a very specific use case, but I am not sure if there is a native support for that. I have prepared a small example to better illustrate the use case:

  1. Suppose we have the following table and records in the database:
Id Name CustomMetadata
0 "first" "{ "CustomPropA": "CustomValA" }"
1 "second" "{ "CustomPropB": "CustomValB" }"
2 "third" "{ "CustomPropA": "CustomValA", "CustomPropC": "CustomValC" }"
  1. CustomMetadata is stored in the DB as a JSON string of user-defined properties without any fixed structure.

Is it possible to enable the filtering for properties which are part of the CustomMetadata field. For example:

  • $filter=CustomMetadata.CustomPropA eq 'CustomValA' should return the first and last records.
  • $filter=CustomMetadata.NewProp eq 'NewVal' should not return any entities.

I went through some of the documentation, but I could not find anything that would suggest support for this case. I found this issue which seems to be somewhat similar, but a follow-up answer is missing.

I am guessing that this might be out-of-scope for the current version of the package, but wanted to hear a second opinion on it.

talalekk avatar Jul 14 '21 14:07 talalekk

How can you pass "$filter=CustomMetadata.CustomPropA eq 'CustomValA'" to the DB and execute the filter clause?

You let the DB to parse the JSON and compare the property name and property value? It's weird.

I spent some time making a solution to meet your requirement.

Here's my DB:

image

Here's my query for whole entity set: (Customers):

http://localhost:5000/odata/customers

image

Here's my query:

image

I uploaded a "hard coding" version here for you to reference.

xuzhg avatar Jul 14 '21 22:07 xuzhg

@talalekk Was your issue resolved?

KenitoInc avatar Apr 19 '22 06:04 KenitoInc