WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

Cast Edm.String to Edm.Int32 in a lambda expresion in a $filter

Open cam-m opened this issue 2 years ago • 3 comments

I have the following entities (highly simplified).

EntityA

  • EntityBList: Collection(EntityB)

EntityB

  • Value: Edm.String

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

[Extent2].[Value] = @p__linq__0)
    ))',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'5'

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:

 EXISTS (SELECT 
        1 AS [C1]
        FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
        WHERE 1 = 0  

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

cam-m avatar Feb 06 '23 02:02 cam-m