[CSHARP-4702] LINQ3 using list.Any(s => m.Field.Contains(s)) fails when Field is null/missing Created: 30/Jun/23  Updated: 28/Oct/23  Resolved: 04/Jul/23

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.21.0

Type: Bug Priority: Unknown
Reporter: John Gonyo 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

Given a model like this:

public Model {
   public List<string> Foo {get; set;}
}

Where the value is null or missing in mongodb.  And a LINQ query like this:

var lookingFor = new List<string> { "value1", "value2" };
var rows = collection.Where(model => lookingFor.Any(value => model.Foo.Contains(value))).ToList();

The query will fail with MongoCommandException: "Command aggregate failed: $in requires an array as a second argument, found: missing."

Rewriting the LINQ query slightly:

var rows = collection.Where(model => model.Foo.Any(value => lookingFor.Contains(value))).ToList() 

And the query succeeds.  This query worked in LINQ2.

 

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

Driver 2.20 using LINQ3

How to Reproduce

See above.

Additional Background

Discovered when LINQ3 is now the default LinqProvider.



 Comments   
Comment by Githook User [ 04/Jul/23 ]

Author:

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

Message: CSHARP-4702: Recognize all Contains methods that are equivalent to Enumerable.Contains.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/aada8e8d422ec522123520b9c526a9319a4b8223

Comment by Robert Stam [ 03/Jul/23 ]

Thanks for reporting this.

The reason it was failing was that the code `model.Foo.Contains(value)` was calling `List<string>.Contains` instead of `Enumerable.Contains` so we weren't recognizing it as being equivalent to `Enumerable.Contains`.

I have a fix in code review.

Comment by PM Bot [ 30/Jun/23 ]

Hi jgonyo@snagajob.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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