[CSHARP-1543] Query Lambdas: "!a.b.Any()" supported, but "a.b.Any() == false" fails Created: 22/Jan/16  Updated: 06/Jul/17  Resolved: 11/Mar/16

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.2
Fix Version/s: 2.2.4

Type: Bug Priority: Minor - P4
Reporter: Kevin Versfeld Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-2012 != operator works incorrect for inter... Closed

 Description   

Recently did a long overdue upgrade from 1.(something) to 2.2, and am having lots of trouble migrating my wrapper code across. One thing I'm noticing is that lots of expressions I had been using previously (via linq) are simply not supported anymore, which is very odd to me. Could you clarify whether this is by design/known/etc?

Works:
collection.Where(mydoc => !mydoc.SomeListProperty.Any())

Does not work:
collection.Where(mydoc => mydoc.SomeListProperty.Any() == false)

I will provide further info on other lambdas that are problematic, later.



 Comments   
Comment by Githook User [ 11/Mar/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1543: Fixed comparison of Any to a constant boolean.
Branch: v2.2.x
https://github.com/mongodb/mongo-csharp-driver/commit/5014111340c4a916b28059a9c89308cbbedcb7e7

Comment by Githook User [ 11/Mar/16 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1543: Fixed comparison of Any to a constant boolean.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/702aaffbf7152ccd212d412672c003b5bf4eb574

Comment by Craig Wilson [ 10/Feb/16 ]

Hi, Kevin.

The fix for it not working in LINQ is part of this ticket: CSHARP-1521. It will be released with version 2.3.0.

We also currently don't have support for injecting FilterExpressions or other such things. We are really trying to get everything working with vanilla linq. At some point, we might need to do this, but I don't feel like we are there yet.

Craig

Comment by Kevin Versfeld [ 09/Feb/16 ]

I'm struggling with how to query a collection looking for documents which have particular values in an array (coming from a Dictionary originally). I believe "ElemMatch" is what I'm after.
I used to use the Query helpers (with ElemMatch) to build a IMongoQuery (if I recall), and then inject it back into my LINQ (using the .Inject() method), but none of that works anymore. (I need it in LINQ form because I'm doing a whole lot of other modifications to the IQueryable, sorting, limiting etc later on)

Anyway, documentation I think indicates I can do something like this to achieve ElemMatch:
collection.Where(doc => doc.StringValues.Any(pair => pair.Key == "somekey")); - OR -
collection.Where(doc => doc.StringValues.Any(pair => pair.Key == "somekey" && pair.Value == "somevalue"));

But both of these just give me the error
"The expression tree is not supported:

{document} {sv}

"
Note that "sv" is the mapped name of the property "StringValues"

Is this something that should be working? Have I misunderstood? Is there still a way to use Inject? (I can post the error I get on that one, if necessary)

I posted this in the User group, but no luck yet:
https://groups.google.com/forum/#!topic/mongodb-user/1zXAJHVkcVQ

Comment by Kevin Versfeld [ 28/Jan/16 ]

Gotcha, thanks.

Comment by Craig Wilson [ 28/Jan/16 ]

Yes, the final version you tried is the proper form for this. We removed the special "ContainsAny" extension method.

Comment by Kevin Versfeld [ 28/Jan/16 ]

Another one: I used to use this in much older versions of driver:

collection.Where(doc => doc.listOfIds.ContainsAny(someOtherList))

which no longer works.
I tried something like this:

collection.Where(doc => doc.listOfIds.Intersect(someOtherList).Any())

which also didn't work, and now use this (which does work):

collection.Where(doc => someOtherList.Any(item => doc.listOfIds.Contains(item)))

Is this expected/correct?

Comment by Kevin Versfeld [ 22/Jan/16 ]

thanks for the super fast response Craig, I will do.

Comment by Craig Wilson [ 22/Jan/16 ]

All expressions should work. Any that don't work are missing a test as the new linq parser passes the old test suite. Please push any more you find to this ticket and we'll tackle them all at once.

Craig

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