[CSHARP-2705] [Regression] Linq Translation of Nested Any throws NotSupportedException Created: 21/Aug/19  Updated: 28/Oct/23  Resolved: 08/Oct/19

Status: Closed
Project: C# Driver
Component/s: Builders, Linq, Serialization
Affects Version/s: 2.8.0, 2.8.1, 2.9.0, 2.9.1, 2.10.0
Fix Version/s: 2.9.3

Type: Bug Priority: Major - P3
Reporter: Taylor Graham Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-2786 Investigate correctness of generated ... Closed

 Description   

Since 2.8.0, translation of LINQ query has broken for me - I now get a NotSupportedException, where before the query translated fine. The query I want is as below:

 

{
    "$or": [
        {
            "Invitees.Organisers": {
                "$in": [
                    "Bob",
                    "John",
                    "Tim",
                    "Ryan"
                ]
            }
        },
        {
            "Invitees.Roles": {
                "$elemMatch": {
                    "k": {
                        "$in": [
                            "Bob",
                            "John",
                            "Tim",
                            "Ryan"
                        ]
                    },
                    "v": {
                        "$in": [
                            "party_planning", "food_preparation"
                        ]
                    }
                }
            },
            "Invitees.BlacklistRoles": {
                "$not": {
                    "$elemMatch": {
                        "k": {
                            "$in": [
                                "Bob",
                                "John",
                                "Tim",
                                "Ryan"
                            ]
                        },
                        "v": {
                            "$in": [
                                "party_planning", "food_preparation"
                            ]
                        }
                    }
                }
            }
        }
    ]
}

 

 To achieve this I use the following link query:

x => 
    x.Invitees.Organisers.Any(a => invitees.Contains(a)) 
|| (
    x.Invitees.Roles.Any(y =>
        invitees.Contains(y.Key) && roles.Any(r => y.Value.Contains(r))) && 
    !(x.Invitees.BlacklistRoles.Any(y =>
        invitees.Contains(y.Key) && roles.Any(r => y.Value.Contains(r))))
    )

The full exception I receive is:

 

System.NotSupportedException: The LINQ expression: {Invitees.Roles}.Where((Contains(value(System.Collections.Generic.List`1[System.String])) AndAlso All(value(System.Collections.Generic.HashSet`1[System.String]).Where(Contains({document}{v}))))) has the member "y" which can not be used to build a correct MongoDB query.

 

 



 Comments   
Comment by Robert Stam [ 15/Nov/22 ]

I was able to verify that the supplied LINQ query translates to the desired filter using LINQ3 but only if the dictionary properties were configured to be represented as an array of key/value pair documents:

private class C
{
    public int Id { get; set; }
    public Invitee Invitees { get; set; }
}
 
private class Invitee
{
    public string[] Organisers { get; set; }
    [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] public Dictionary<string, string[]> Roles { get; set; }
    [BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] public Dictionary<string, string[]> BlacklistRoles { get; set; }
}

`$elemMatch` only works with arrays.

Comment by Githook User [ 08/Oct/19 ]

Author:

{'name': 'Dmitry Lukyanov', 'username': 'DmitryLukyanov', 'email': 'dmitry.lukyanov@mongodb.com'}

Message: CSHARP-2705: Linq Translation of Nested Any throws NotSupportedException.
Branch: v2.9.x
https://github.com/mongodb/mongo-csharp-driver/commit/0543c63ef8b17d3ed5a22e59dcaf14705d7618a3

Comment by Githook User [ 08/Oct/19 ]

Author:

{'username': 'DmitryLukyanov', 'email': 'dmitry.lukyanov@mongodb.com', 'name': 'Dmitry Lukyanov'}

Message: CSHARP-2705: Linq Translation of Nested Any throws NotSupportedException.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/84d429e2b174e0481b37ef8f07a12b54aa251395

Comment by Taylor Graham [ 21/Aug/19 ]

This might be linked to CSHARP-2699

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