Skip to content
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

Added ArrayContains to CosmosLinqExtensions to allow partial matching #4992

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bcrobinson
Copy link

Pull Request Template

Description

The Array_Contains function CosmosDB Sql has a 3rd parameter which allows it to do a partial match on the given item. This is unable to be called with the built in Linq array.Contains(item) extension methods.

This adds this adds an explicit mapping to this function to allow it to be called in Linq like this:

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

Remarks

ArrayContains name instead of Contains

I namef the new CosmosLinqExtensions extension method ArrayContains instead of Contains as the other extensions methods matched thier Cosmos Sql counterparts and because this is for a direct invokation of this function.

Can change the name if you require.

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

The itemToMatch paramater is an object as it will need to take anonymous objects (and c# doesn't have a similar concept to typescript's Partial<T>). If was generic callers would be force to pass in an object of the same type, and the default serialiser creates object with every property that exists on the class. So the created query object with default values (e.g. Array_Contains(root["Items"], { "I" = 0, "s" = null }) )which would the cause Cosmos to try and match on all properties.

Because it's not generic the obj is then IEnumerable, so it would at a minimum only work on iterable values.

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),

Modification to ArrayContainsVisitor

The ArrayContainsVisitor visitor already exists for the Contains Linq mappings, the I modified this to work with the different extension version for Array Contains.

Type of change

  • New feature (non-breaking change which adds functionality)

Closing issues

closes #4991

To automatically close an issue: closes #IssueNumber

The `Array_Contains` funtion CosmosDB Sql has a 3rd parameter which allows it to do a partial match on the given item. This is unable to be called with the built in Linq `array.Contains(item)` extension methods.

This adds this adds an explicit mapping to this function to allow it to be called in Linq like this:
`documents.Where(document => document.ObjectArray.ArrayContains(new { Name = "abc" }, true))`
@bcrobinson bcrobinson force-pushed the feature/array_contains_builtin_function branch from 3c89ee6 to e7785f8 Compare January 31, 2025 16:01
@bcrobinson
Copy link
Author

@microsoft-github-policy-service agree company="Solarvista Software LTD"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Array_Contains partial match support to Linq
1 participant