Skip to content

LINQ: Fixes Dictionary.Any() to generate OBJECTTOARRAY SQL#5595

Merged
Praveen-Msft merged 1 commit intousers/kirankk/fix-issue-5547-linq-dictionary-objecttoarrayfrom
copilot/sub-pr-5583
Feb 2, 2026
Merged

LINQ: Fixes Dictionary.Any() to generate OBJECTTOARRAY SQL#5595
Praveen-Msft merged 1 commit intousers/kirankk/fix-issue-5547-linq-dictionary-objecttoarrayfrom
copilot/sub-pr-5583

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 2, 2026

The LINQ translator was generating invalid SQL for Dictionary<string, object>.Any() queries. It treated dictionaries as standard enumerables and used JOIN instead of Cosmos DB's required OBJECTTOARRAY() function.

Root Cause

TypeSystem.IsEnumerable() returns true for Dictionary<K,V> (via IEnumerable<KeyValuePair<K,V>>), causing ExpressionToSQL to translate dictionary access as a standard collection JOIN operation.

Changes

  • TypeSystem.cs: Added IsDictionary() check to identify dictionary types
  • ExpressionToSQL.cs: Wrap dictionary member access with OBJECTTOARRAY() conversion
  • LinqDictionaryQueryTests.cs: Added tests for dictionary query translation

Example

// Query with Dictionary property
var query = container.GetItemLinqQueryable<MyDoc>()
    .Where(d => d.AdditionalData.Any(kvp => kvp.Value == searchFor));

Before (invalid SQL):

SELECT * FROM c 
WHERE EXISTS(SELECT VALUE x FROM x IN c.AdditionalData WHERE x.v = @searchFor)

After (correct SQL):

SELECT * FROM c 
WHERE EXISTS(SELECT VALUE x0 FROM x0 IN (SELECT VALUE ObjectToArray(c["AdditionalData"])) 
             WHERE x0["v"] = @searchFor)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix Dictionary.Any() to generate correct SQL with OBJECTTOARRAY LINQ: Fix Dictionary.Any() to generate OBJECTTOARRAY SQL Feb 2, 2026
Copilot AI requested a review from Praveen-Msft February 2, 2026 17:13
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good!

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the required format: "[Internal] Category: (Adds|Fixes|Refactors|Removes) Description"

Internal should be used for PRs that have no customer impact. This flag is used to help generate the changelog to know which PRs should be included. Examples:
Diagnostics: Adds GetElapsedClientLatency to CosmosDiagnostics
PartitionKey: Fixes null reference when using default(PartitionKey)
[v4] Client Encryption: Refactors code to external project
[Internal] Query: Adds code generator for CosmosNumbers for easy additions in the future.

@Praveen-Msft Praveen-Msft marked this pull request as ready for review February 2, 2026 17:18
@Praveen-Msft
Copy link
Copy Markdown
Contributor

Please follow the required format: "[Internal] Category: (Adds|Fixes|Refactors|Removes) Description" Internal should be used for PRs that have no customer impact. This flag is used to help generate the changelog to know which PRs should be included. Examples: Diagnostics: Adds GetElapsedClientLatency to CosmosDiagnostics PartitionKey: Fixes null reference when using default(PartitionKey) [v4] Client Encryption: Refactors code to external project [Internal] Query: Adds code generator for CosmosNumbers for easy additions in the future.

@copilot update the title of the PR by following above format

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Feb 2, 2026

Please follow the required format: "[Internal] Category: (Adds|Fixes|Refactors|Removes) Description" Internal should be used for PRs that have no customer impact. This flag is used to help generate ...

I'm unable to directly update PR titles as I don't have access to modify GitHub PR metadata.

Based on the required format and the fix this PR provides (customer-facing bug fix in LINQ query translation), the title should be:

Query: Fixes Dictionary.Any() to generate correct SQL with OBJECTTOARRAY

A maintainer will need to manually update the PR title.

Copilot AI requested a review from Praveen-Msft February 2, 2026 17:21
@Praveen-Msft Praveen-Msft changed the title LINQ: Fix Dictionary.Any() to generate OBJECTTOARRAY SQL LINQ: Fixes Dictionary.Any() to generate OBJECTTOARRAY SQL Feb 2, 2026
@Praveen-Msft Praveen-Msft merged commit 1295048 into users/kirankk/fix-issue-5547-linq-dictionary-objecttoarray Feb 2, 2026
5 of 7 checks passed
@Praveen-Msft Praveen-Msft deleted the copilot/sub-pr-5583 branch February 2, 2026 17:23
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.

2 participants