[CSHARP-4592] LINQ3 projections unable to translate indexer access in IDictionary property Created: 31/Mar/23  Updated: 28/Oct/23  Resolved: 05/Apr/23

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.19.1
Fix Version/s: 2.19.2

Type: Bug Priority: Unknown
Reporter: Brandon N/A Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

After updating to the latest version of the mongodb driver (2.19), which switched to the LINQ3 queryable provider, an existing query is throwing with the exception:

MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: x.Dict.get_Item("test").    at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators.GetItemMethodToAggregationExpressionTranslator.Translate(TranslationContext context, MethodCallExpression expression)

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

MongoDBDriver 2.19.1, running against a local server version 6.0.5

How to Reproduce

Example console app:

using MongoDB.Driver;
using MongoDB.Driver.Linq;
 
var Query = (LinqProvider provider) =>
{
    var connectionstring = "mongodb://docker:mongopw@localhost:49153";
    var settings = MongoClientSettings.FromConnectionString(connectionstring);
    settings.LinqProvider = provider;
    var client = new MongoClient(settings);
    var db = client.GetDatabase("db1");
    var collection = db.GetCollection<ExampleClass>("collection1");
    return collection.AsQueryable().Select(x => x.Dict["test"]).First();
};
 
Console.WriteLine(Query(LinqProvider.V2)); // Works
Console.WriteLine(Query(LinqProvider.V3)); // Throws exception
 
class ExampleClass
{
    public IDictionary<string, int> Dict { get; set; }
}

Additional Background

Tried to do some investigation, from what I can tell, GetItemMethodToAggregationExpressionTranslator.Translate calls TryGetIDictionaryGenericInterface but there is no check that the `type` parameter itself is an `IDictionary` like `TryGetIEnumerableGenericInterface` does.



 Comments   
Comment by Githook User [ 18/May/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4592: Support indexer for IDictionary (as well as Dictionary).
Branch: v2.19.x
https://github.com/mongodb/mongo-csharp-driver/commit/77da1b352f12b7a986ce0beb2e315004d5014998

Comment by Githook User [ 05/Apr/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4592: Support indexer for IDictionary (as well as Dictionary).
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/640a41735338559629f7ff91b90f439b2467b361

Comment by Brandon N/A [ 05/Apr/23 ]

Thanks for the fix!

Comment by Robert Stam [ 03/Apr/23 ]

Thanks for reporting this.

Your diagnosis was spot on also. Thanks for making the extra effort!

Generated at Wed Feb 07 21:48:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.