odata.net icon indicating copy to clipboard operation
odata.net copied to clipboard

.Contains() should be supported

Open nickkin-msft opened this issue 10 years ago • 3 comments

Any .Contains() in a .Where() clause, could easily be decomposed by the OData client's IQueryable expression tree visitor into a list of "ORed" conditionals, even if .Contains() is not strictly supported by OData directly with an equivalent URI.

This is at least, or especially, true if the list is a simple value type list rather than a list of complex.

Assemblies affected

Microsoft.OData.Client v4.0.30319 Microsoft.OData.Core v4.0.30319 Microsoft.OData.Edm v4.0.30319

Reproduce steps

Execute the following or equivalent against any OData endpoint using the standard T4 generated client.

var values = new string[] { "val1", "val2" }; odataClient.MyCollection.Where(x => list.Contains(x.prop))

Expected result

The list.Contains() clause should be converted into a series of "ORed" conditionals in the generated URI which is fully supported by OData and allows easier use of the client.

Actual result

Depending on how you execute .Contains(), either NotSupportedException or a more confusing variant (see issues/608).

nickkin-msft avatar Jun 29 '16 00:06 nickkin-msft

I just ran into this, trying to filter an entity set by a collection of in-memory values.

Per the example:

var values = new string[] { "val1", "val2" };
odataClient.MyCollection.Where(x => values.Contains(x.prop))

My expectation was that this gets translated to in.

GET /odata/MyCollection?$filter=prop in ('val1', 'val2')

Is is possible to add this behavior? Is there a workaround?

sliekens avatar Aug 24 '20 10:08 sliekens

There are two Contains methods that result in different behavior:

  • ICollection<T>.Contains() which is implemented by List<T>
  • Enumerable.Contains<TSource>(this IEnumerable<TSource> source, TSource value) for arrays and other enumerables

The latter was easy to implement but ICollection.Contains() is not so easy to parse because the method itself isn't a generic method.

sliekens avatar Sep 09 '20 00:09 sliekens

Fixed by https://github.com/OData/odata.net/pull/1868

gathogojr avatar Mar 13 '22 11:03 gathogojr