[CSHARP-2012] != operator works incorrect for internal collection Created: 06/Jul/17  Updated: 28/Oct/23  Resolved: 15/Jan/19

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

Type: Bug Priority: Critical - P2
Reporter: anatolii petruchok Assignee: Dmitry Lukyanov (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

C# linqu quwery


Issue Links:
Related
related to CSHARP-2460 Support more difficult "Any"s which a... Closed
related to CSHARP-1356 LINQ .Any(x => !list.Contains(x)) tra... Closed
related to CSHARP-1543 Query Lambdas: "!a.b.Any()" supported... Closed
Backwards Compatibility: Minor Change

 Description   

I have collection with element(let it be only one in collection)

{ 
    "_id" : ObjectId("595e083dfe3d9236b8c178bf"), 
    "Version" : NumberInt(0), 
    "CompanyId" : ObjectId("579a15affe3d910de824ea00"), 
    "TenderAlertLotStateList" : [
        {
            "TenderLotId" : ObjectId("595e077bfe3d9236b8c175be"), 
            "ClientResponseDescription" : {
                "ResponseBy" : null, 
                "ResponseAt" : null, 
                "ClientResponseType" : NumberInt(2), 
                "TenderAlertRejectionReasonId" : null
            }, 
            "TenderAlertStatus" : NumberInt(0)
        }, 
        {
            "TenderLotId" : ObjectId("595e0765fe3d9236b8c1759f"), 
            "ClientResponseDescription" : {
                "ResponseBy" : null, 
                "ResponseAt" : null, 
                "ClientResponseType" : NumberInt(2), 
                "TenderAlertRejectionReasonId" : null
            }, 
            "TenderAlertStatus" : NumberInt(0)
        }, 
        {
            "TenderLotId" : ObjectId("595e074bfe3d9236b8c1757b"), 
            "ClientResponseDescription" : {
                "ResponseBy" : null, 
                "ResponseAt" : null, 
                "ClientResponseType" : NumberInt(2), 
                "TenderAlertRejectionReasonId" : null
            }, 
            "TenderAlertStatus" : NumberInt(0)
        }, 
        {
            "TenderLotId" : ObjectId("595e06eefe3d9236b8c173a9"), 
            "ClientResponseDescription" : {
                "ResponseBy" : "apetruc1@gmail.com", 
                "ResponseAt" : ISODate("2017-07-06T09:54:52.437+0000"), 
                "ClientResponseType" : NumberInt(1), 
                "TenderAlertRejectionReasonId" : null
            }, 
            "TenderAlertStatus" : NumberInt(2) //<<it is TenderAlertStatus.Archived!!!
        }
    ]
}

1.

GetAllFor().Where(a => a.CompanyId == clientCompanyId && a.TenderAlertLotStateList.Any(s => s.TenderAlertStatus != TenderAlertStatus.Archived)).ToList()

returns 0 elements. Inncorrect!
2.

GetAllFor().Where(a => a.CompanyId == clientCompanyId && a.TenderAlertLotStateList.Any(s => !(s.TenderAlertStatus == TenderAlertStatus.Archived))).ToList()

returns 0 elements. Inncorrect!
3.

GetAllFor().Where(a => a.CompanyId == clientCompanyId && a.TenderAlertLotStateList.Any(s => !a.TenderAlertLotStateList.All(s => s.TenderAlertStatus == TenderAlertStatus.Archived))).ToList()

Even throws error. Inncorrect!

4.

GetAllFor().Where(a => a.CompanyId == clientCompanyId && a.TenderAlertLotStateList.Any(s => !s.TenderAlertStatus.Equals(TenderAlertStatus.Archived))).ToList()

returns the element. Correct. Previous 2 should works in same way
5.

GetAllFor().Where(a => a.CompanyId == clientCompanyId && a.TenderAlertLotStateList.Any(s => s.TenderAlertStatus == TenderAlertStatus.Archived))).ToList()

returns the element. Correct. And also it the same syntaxes as in 1st. Only difference between 5 and 1 is "==" vs "!=".
It means "!=" operator does not work properly for internal collection

Note GetAllFor() returns IQueryable<of the class>



 Comments   
Comment by Githook User [ 15/Jan/19 ]

Author:

{'username': 'dmitry-lukyanov', 'email': 'dmitry_lukyanov@epam.com', 'name': 'Dmitry Lukyanov'}

Message: CSHARP-2012: Fix Any workflow with ne operator.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/1a49c6ead1bc63060935be3f15f1b6dbae142333

Comment by Dmitry Lukyanov (Inactive) [ 21/Dec/18 ]

Hi apetruc@gmail.com,

The 3rd example from this ticket has been moved into the ticket CSHARP-2460 with the changed description as there was the syntax error in the query. Also, we do not fully support the All linq method in case we have a way to reach the filtering target via the Any, so the ticket description was changed.

Please let me know if you have questions about the new ticket.

Comment by Craig Wilson [ 06/Jul/17 ]

Thanks for reporting Anatolii,

Sorry you are having trouble. The Any operator is interesting to get right with MongoDB. There are a few tickets related to Any already. I'll link them here.

Craig

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