-
Notifications
You must be signed in to change notification settings - Fork 473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cast Edm.String to Edm.Int32 in a lambda expresion in a $filter #2746
Comments
I assume EntityB/Value is declared as type string in the OData Model, Entity Framework and SQL. But strings cannot be cast (i.e. type conversion) to a number. One would need to parse the string into a number. |
Yes the property is declared as a string throughout the app and is used to hold string representations of various types of values ( dates, numbers, strings) The OData lists support for casting an Edm.String to any Primitive type provided the string contains a valid representation of the targeted Primitive. Excerpt from section 5.1.1.10 of the OData Version 4.01 Part 2 URL Conventions
And AFAIK a 'string' can be cast to an INT in SQL Server. E.g. So if the answer is that point 5 of OData's cast function is not supported for whatever reason thats cool, but the behaviour I'm seeing is less than ideal. At the very least if its unsupported, an error message to the effect should be thrown. As it is you get a http 200 success with no results because of the expression Yes, you're spot on, we could migrate our DB structure, or, more attractive, create a View with values cast or converted to their respective types. However the effort to do this can be avoided if this lib supports the OData spec in respect to cast. |
I apologize, I made too many assumptions based on other languages. Thanks a lot for the link to the spec. I consider this a bug given that information now and we triage again. |
I have the following entities (highly simplified).
EntityA
EntityB
The Value property of EntityB can store values that can be cast to numbers, but also values that are arbitrary strings.
I want to filter Entity 1 based on these values using a Lambda expression like this:
EntityAs?$filter=EntityB/any(link:(cast(link/Value, Edm.Int32) gt 5))
This works fine
EntityAs?$filter=EntityB/any(link:(cast(link/Value, Edm.String) eq '5'))
and generates SQL like this
This
EntityAs?$filter=EntityB/any(link:(cast(link/Value, Edm.String) gt '5'))
is not going to work for a numeric comparison (E.g. The expression '100' gt '5' returns false when compared as strings)
Casting the value to an Int.32 like this:
EntityAs?$filter=EntityB/any(link:(cast(link/Value, Edm.Int32) eq 5))
...results in the lambda filter expression being removed from the EF6 generated SQL, and replaced with this:
Is there anything I can do to make this work?
Assemblies affected
Microsoft.AspNet.OData 7.6.3
Microsoft.OData.Core 7.14.0
Microsoft.OData.Edm 7.14.0
Reproduce steps
See above
Expected result
Should be able to cast a string to an int (according to OData v4 specs
Actual result
Cast string to an int doesn't work.
Additional detail
N/A
The text was updated successfully, but these errors were encountered: