-
Notifications
You must be signed in to change notification settings - Fork 507
Query: Adds ArrayContains to CosmosLinqExtensions to allow partial matching on array fields in queries #4992
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
base: master
Are you sure you want to change the base?
Changes from all commits
786db81
d2e3e99
9d99782
c981452
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
namespace Microsoft.Azure.Cosmos.Linq | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
@@ -237,6 +238,62 @@ public static bool RegexMatch(this object obj, string regularExpression, string | |
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a boolean indicating whether the array contains the specified value. | ||
/// You can check for a partial or full match of an object by using a boolean expression within the function. | ||
/// For more information, see https://learn.microsoft.com/en-gb/azure/cosmos-db/nosql/query/array-contains. | ||
/// This method is to be used in LINQ expressions only and will be evaluated on server. | ||
/// There's no implementation provided in the client library. | ||
/// </summary> | ||
/// <param name="obj"></param> | ||
/// <param name="itemToMatch">The value to search within the array.</param> | ||
/// <returns>Returns true if the array array contains the specified value; otherwise, false.</returns> | ||
/// <example> | ||
/// <code> | ||
/// <![CDATA[ | ||
/// var matched = documents.Where(document => document.Namess.ArrayContains(<itemToMatch>, <partialMatch>)); | ||
/// // To do a partial match on an array of objects, pass in an anonymous object set partialMatch to true | ||
/// var matched = documents.Where(document => document.ObjectArray.ArrayContains(new { Name = <name> }, true)); | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
public static bool ArrayContains(this IEnumerable obj, object itemToMatch) | ||
{ | ||
// The signature for this is not generic so the user can pass in anonymous type for the item to match | ||
// e.g documents.Where(document => document.FooItems.ArrayContains(new { Name = "Bar" }, true) | ||
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this Array_contains is functionally the same as IEnumerable.Contains(), so please remove this and rename the other extension method to Contains instead |
||
/// <summary> | ||
/// Returns a boolean indicating whether the array contains the specified value. | ||
/// You can check for a partial or full match of an object by using a boolean expression within the function. | ||
/// For more information, see https://learn.microsoft.com/en-gb/azure/cosmos-db/nosql/query/array-contains. | ||
/// This method is to be used in LINQ expressions only and will be evaluated on server. | ||
/// There's no implementation provided in the client library. | ||
/// </summary> | ||
/// <param name="obj"></param> | ||
/// <param name="itemToMatch">The value to search within the array.</param> | ||
/// <param name="partialMatch">Indicating whether the search should check for a partial match (true) or a full match (false).</param> | ||
/// <returns>Returns true if the array array contains the specified value; otherwise, false.</returns> | ||
/// <example> | ||
/// <code> | ||
/// <![CDATA[ | ||
/// var matched = documents.Where(document => document.Namess.ArrayContains(<itemToMatch>, <partialMatch>)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/// // To do a partial match on an array of objects, pass in an anonymous object set partialMatch to true | ||
/// var matched = documents.Where(document => document.ObjectArray.ArrayContains(new { Name = <name> }, true)); | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
public static bool ArrayContains(this IEnumerable obj, object itemToMatch, bool partialMatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
// The signature for this is not generic so the user can pass in anonymous type for the item to match | ||
// e.g documents.Where(document => document.FooItems.ArrayContains(new { Name = "Bar" }, true) | ||
// | ||
// Has 2 overloads instead of partialMatch with default value (i.e `bool partialMatch = false`) as | ||
// default values cannot be used in expressions (error CS0854), and this method will only be used in expressions | ||
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented); | ||
} | ||
|
||
/// <summary> | ||
/// This method generate query definition from LINQ query. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<Results> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with int value]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ArrayField.ArrayContains(Convert(1, Object)))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ArrayField"], 1) | ||
FROM root]]></SqlQuery> </Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with int value]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ArrayField.ArrayContains(Convert(1, Object)))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ArrayField"], 1)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with object value]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc")))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with object value]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc")))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"})]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with int value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ArrayField.ArrayContains(Convert(1, Object), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ArrayField"], 1, true) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with int value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ArrayField.ArrayContains(Convert(1, Object), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ArrayField"], 1, true)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with object value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, true) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with object value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, true)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with int value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ArrayField.ArrayContains(Convert(1, Object), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ArrayField"], 1, false) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with int value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ArrayField.ArrayContains(Convert(1, Object), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ArrayField"], 1, false)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with object value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, false) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with object value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, false)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
</Results> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if this is necessary