[CSHARP-2247] Support array.Contains(value) == false in LINQ queries Created: 19/Apr/18  Updated: 03/Nov/22  Resolved: 03/Nov/22

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

Type: New Feature Priority: Major - P3
Reporter: Vijay Devappa Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: rfw
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-3234 InvalidOperationException when check ... Closed
Related
is related to CSHARP-462 Linq doesn't support "In" clause Closed

 Description   

There is no way to do a contains with the Mongo LINQ driver. This makes it very hard to do queries like the below:

 
var list = (from p in collection.AsQueryable<MyBaseData>()
                where p.Id != null &&
                           ignoredIds.Contains(p.Id) == false &&
                           p.Reference != "Baked Goods"    
                select p).ToList();



 Comments   
Comment by Vijay Devappa [ 30/Jan/19 ]

Makes sense. Thanks for the workaround. I use the "false" form because it is more readable when we look at a mass of code later.

Comment by Robert Stam [ 30/Jan/19 ]

Leaving this ticket open because we should support both of the following as being equivalent:

!array.Contains(value)
array.Contains(value) == false

Currently we only support the first form.

 

Comment by Wan Bachtiar [ 29/Jan/19 ]

There is no way to do a contains with the Mongo LINQ driver

Hi Vijay,

The problem here is the way you express Not Contains(). For example, given the following documents in a collection:

{"_id": "a", "Reference": "foo"},
{"_id": "b", "Reference": "bar"},
{"_id": "c", "Reference": "baz"},
{"_id": "d", "Reference": "qux"}

Based on your example, let's declare an example class Foo as below:

public class Foo 
{
    public string Id {get;set;}
    public string Reference {get;set;}
}

Utilise the following snippet LINQ expression to achieve the same way:

string[] ignoredIds = new string[1] {"b"}; 
var documents = (from p in collection.AsQueryable<Foo>() where 
                                        p.Id !=null && 
                                        !ignoredIds.Contains(p.Id) &&
                                        p.Reference != "baz"
                select p).ToList();

Which returns the following documents (where Id is not null and Id is not in ignoredIds and Reference is not baz)

{ "_id" : "a", "Reference" : "foo" }
{ "_id" : "d", "Reference" : "qux" }

The above snippet should work for both MongoDB .NET/C# driver v2.5 and v2.7.

Please note that the CSHARP project is for reporting bugs or feature suggestions for the MongoDB .NET/C# driver. If you have any follow-up questions on the use of the driver, please post a question on mongodb-user group with relevant the information.

Regards,
Wan.

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