[CSHARP-1771] Support IIF method (i.e. ternary operator) in LINQ Created: 25/Sep/16 Updated: 08/Dec/21 Resolved: 25/Oct/21 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq, LINQ3 |
| Affects Version/s: | 2.3 |
| Fix Version/s: | 2.14.0 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Omri Cohen | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 11 |
| Labels: | linq, linq,query, rp-track | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
win 10, .net 4.6.1 |
||
| Issue Links: |
|
||||||||||||||||||||||||||||||||
| Case: | (copied to CRM) | ||||||||||||||||||||||||||||||||
| Description |
|
I'm trying to use the new driver to support an OData webapi service.
|
| Comments |
| Comment by Robert Stam [ 25/Oct/21 ] | |||||||||||||||||
|
This issue has been fixed in the new LINQ provider (known as LINQ3) which will be included in the upcoming 2.14 release. Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality. To configure a client to use the LINQ3 provider use code like the following
| |||||||||||||||||
| Comment by Githook User [ 25/Oct/21 ] | |||||||||||||||||
|
Author: {'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}Message: | |||||||||||||||||
| Comment by Jean-Claude ADIBA [ 30/Sep/21 ] | |||||||||||||||||
|
I have been waiting for this now for over a month | |||||||||||||||||
| Comment by Adam Jodlowski [ 30/Sep/21 ] | |||||||||||||||||
|
Hello Can i ask you if you have any rough estimate as to when this new LINQ implementation will be finished and released? I'm experiencing the problem with IIF and i need to create some workaround for that. It would be nice to know for how much long i will need to support this workaround. Thanks. | |||||||||||||||||
| Comment by Dimitri Kroo [ 08/Jun/21 ] | |||||||||||||||||
|
Hi, mega news! Thank you! "Upcoming beta" means it is not in beta1 yet? | |||||||||||||||||
| Comment by James Kovacs [ 07/Jun/21 ] | |||||||||||||||||
|
We are currently completing a code review of the new LINQ implementation before merging it into the main repository. Our goal is to ship it as an opt-in replacement for our current LINQ provider in an upcoming beta release of 2.13.0. Sincerely, | |||||||||||||||||
| Comment by Jiri Vavra [ 07/Jun/21 ] | |||||||||||||||||
|
Hello Can i ask you if you have any rough estimate as to when this new LINQ implementation will be finished and released? I'm experiencing the problem with IIF and i need to create some workaround for that. It would be nice to know for how much long i will need to support this workaround. Thanks.
| |||||||||||||||||
| Comment by Dmitry Lukyanov (Inactive) [ 15/Apr/21 ] | |||||||||||||||||
|
Hello dimitri.kroo@baramundi.com , we're actively working on a new LINQ implementation, and consider adding this case there. We will let here know when it will be ready. | |||||||||||||||||
| Comment by Dimitri Kroo [ 15/Apr/21 ] | |||||||||||||||||
|
Hi! Any Update on this? | |||||||||||||||||
| Comment by Robert Stam [ 10/Dec/20 ] | |||||||||||||||||
|
I don't think it's possible to translate IIF to a normal MongoDB query/filter. There simply is no available query operator that could behave as required. It would be possible however to translate it to a query/filter using the `$expr` escape mechanism to use the full expression language in a normal query. | |||||||||||||||||
| Comment by James Kovacs [ 10/Dec/20 ] | |||||||||||||||||
|
Note that ExpressionType.Conditional (aka the C# ternary operator aka IIF) works in the select clause but not the where clause: Works:
Fails with NotImpementedException:
| |||||||||||||||||
| Comment by Robert Stam [ 05/Oct/20 ] | |||||||||||||||||
|
Thank you for reporting this. We are working on a new implementation of our LINQ translator and will make sure IIF works properly in the new implementation. | |||||||||||||||||
| Comment by Nicholas Burnett [ 07/Jan/20 ] | |||||||||||||||||
|
I was running into a similar problem when specifying an OData filter like this: $filter=isof('DerivedType') Here is the exception this caused: 'Unsupported filter: IIF(({document} Is DerivedType), True, False).' Setting HandleNullPropagation to False does not solve this problem. So adding my vote for IFF being properly supported. However, I was able to work around the issue like this: Create a filter binder that unwraps the 'is' expression from the 'IFF' expression.
And then configuring this FilterBinder in IoC like this:
| |||||||||||||||||
| Comment by Luke Cooper [ 06/Apr/19 ] | |||||||||||||||||
|
My case is that I have an optional property that is a class. I want it to deserialize to null if it doesn't exist. So, the query ends up looking like Doesn't work: { Name: "something", SomeInt: 2 } | |||||||||||||||||
| Comment by Ross Buggins [ 30/Oct/18 ] | |||||||||||||||||
|
Spot on - _settings.HandleNullPropagation = HandleNullPropagationOption.False has done the trick for me too - thanks for letting us know. Ross | |||||||||||||||||
| Comment by Terence Hancock [ 03/Jul/18 ] | |||||||||||||||||
|
By the way, I have found a workaround to this in WebApi OData. IIF((({document}{Description} == null) OrElse False), null, Convert({document}{Description}.Contains("SUBSTRING"))) is not supported.I simply turned off NullPropagation in the OdataQuerySettings like so:
And now the .Contains works great! IIF is apparently a null handling part of the expression and I would think it SHOULD be handled by the IMongoQueryable implementation. Although I would admit my knowledge of expressions and linq is limited, so I could be wrong there. I have also found that setting the _settings.EnsureStableOrdering = false; is helpful with using OrderBy | |||||||||||||||||
| Comment by Teun Kooijman [ 10/Jun/18 ] | |||||||||||||||||
|
Hi Omri,
Do you perhaps have the custom .dll out on GitHub somewhere, or perhaps on a NuGet repository I could access? Would love to see what your version is capable of!
Kind Regards, Teun | |||||||||||||||||
| Comment by Omri Cohen [ 10/Jun/18 ] | |||||||||||||||||
|
My fixes have updated to much more than described on my last comment. If any of MongoDB's developers would like me to write my changes and discuss how to merge it in, I would love to help. Cheers, | |||||||||||||||||
| Comment by Teun Kooijman [ 08/Jun/18 ] | |||||||||||||||||
|
@Omri Cohen's fix seems easy enough, but I'd hate to use a custom .dll for something so trivial. Is there any chance this is going to be fixed by the MongoDb C# Driver team themselves anytime soon? | |||||||||||||||||
| Comment by Omri Cohen [ 28/Dec/17 ] | |||||||||||||||||
|
I forked your github repo and added few lines to the start of the Translate method and now everything works if (binary.Left is UnaryExpression leftUnary) { node = leftUnary; } } FilterDefinition<BsonDocument> filter = null; switch (node.NodeType) @Robert Stam, any chance you'll fix this issue for the near future releases? Thanks and Merry Xmas, | |||||||||||||||||
| Comment by Omri Cohen [ 21/Dec/17 ] | |||||||||||||||||
|
Any news on this ticket? | |||||||||||||||||
| Comment by Alan Clark [ 24/Dec/16 ] | |||||||||||||||||
|
Another vote for this - OData filters contains, isof, startswith (and presumably others, haven't tried) can't be run against a mongo IQueryable because they require IIF support. | |||||||||||||||||
| Comment by Robert Stam [ 04/Oct/16 ] | |||||||||||||||||
|
The driver already supports LINQ. This ticket is specifically about the IIF method not being supported by the .NET driver's LINQ implementation. | |||||||||||||||||
| Comment by Omri Cohen [ 04/Oct/16 ] | |||||||||||||||||
|
You don't have to implement OData, just LINQ | |||||||||||||||||
| Comment by Ivan Artemov [ 03/Oct/16 ] | |||||||||||||||||
|
I'm against! I think there is no need to clutter up the C# driver with OData implementation. | |||||||||||||||||
| Comment by Omri Cohen [ 30/Sep/16 ] | |||||||||||||||||
|
Thanks Craig, OData support can benefit mongo with new customers that wants the power of OData with the power of document db as mongo. | |||||||||||||||||
| Comment by Craig Wilson [ 28/Sep/16 ] | |||||||||||||||||
|
Thanks Omri, It appears as though we don't support this particular expression (IIF). I'll mark this as a new Feature and we'll see about implementing it. Craig |