Skip to content

Add Array_Contains partial match support to Linq #4991

Open
@bcrobinson

Description

@bcrobinson

Is your feature request related to a problem? Please describe.

The Array_Contains function CosmosDB Sql has a 3rd parameter which allows it to do a partial match on the given item.

https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/array-contains

The current Linq array.Contains(item) don't have a way to set this 3rd parameter to do the partial match.

Describe the solution you'd like

A Linq extension method that allows me to set all the CosmosSql Array_Contains function parameters, something like this:

documents.Where(document => document.ObjectArray.ArrayContains(new { Name = "abc" }, true))

This would most likely need to be a new extension method where the given item has an object type, as the partial match would need to take an anonumous object so we can only supply the properties we need. If it uses the generic Linq .Contains<T>(...) extension methods it would require creating instances of that class, which would then serialise with null property values.

I.e. it would need to look like this

bool ArrayContains(this IEnumerable obj, object itemToMatch, bool partialMatch)

As if this was ArrayContains<T>(this IEnumerable<T> obj, T itemToMatch, bool partialMatch) then we'd need to always pass the same T type to this method. The downside is you loose some type checking (the anonymous object could have a property name typed wrong),

Describe alternatives you've considered
We can create a user defined function in cosmos that wraps the Array_Contains call, but we ran into issues with it not using indexes in searches.

Additional context

I had a quick play around to see if I could add this to the client lib, if this is a valid request I can create a PR for this new extension method

https://github.com/bcrobinson/azure-cosmos-dotnet-v3/tree/feature/array_contains_builtin_function

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions