[CSHARP-462] Linq doesn't support "In" clause Created: 03/May/12 Updated: 15/Nov/21 Resolved: 09/May/12 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.4.2 |
| Fix Version/s: | 1.5 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Brian | Assignee: | Craig Wilson |
| Resolution: | Done | Votes: | 3 |
| Labels: | None | ||
| Σ Remaining Estimate: | Not Specified | Remaining Estimate: | Not Specified |
| Σ Time Spent: | Not Specified | Time Spent: | Not Specified |
| Σ Original Estimate: | Not Specified | Original Estimate: | Not Specified |
| Issue Links: |
|
||||||||||||
| Sub-Tasks: |
|
||||||||||||
| Description |
|
public sealed class Document public List<string> Folders { get; set; } } } 1. var documentFolders = documentCollection.AsQueryable().Where(x => x.Id == "document1").Select(x => x.Folders).Single(); Stack trace: at MongoDB.Driver.Linq.SelectQuery.BuildQuery(Expression expression) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Linq\Translators\SelectQuery.cs:line 862 |
| Comments |
| Comment by Vijay Devappa [ 26/Feb/18 ] | ||||||
|
It is strange, but I got this error using contains with the 2.5 nuget package
The error is: 2/26 5:32:01 PM>TID:18|CPU:34.81541|worker000000|worker000000$|Worker Service Fabric|error|DataGenerator::GenerateData()|LIN:0|********************************************************** ERROR: System.InvalidOperationException: Contains(value(System.Collections.Generic.List`1[System.Nullable`1[Locus.MongoDB.ClientWarehouse.ReportRequestSource]])) is not supported. at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetFieldExpression(Expression expression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression) at MongoDB.Driver.Linq.MongoQueryableImpl`2.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at | ||||||
| Comment by Craig Wilson [ 09/May/12 ] | ||||||
|
This has been merged to master. | ||||||
| Comment by Craig Wilson [ 09/May/12 ] | ||||||
|
That is a good idea. However, an Intersection produces another set, not a boolean value. Doing this would violate the intention of the Intersect method. In addition, the compiler wouldn't like this. One possibility would be to use it in conjunction with the Any method; (c => c.List.Intersect(local).Any()) or (c => local.Intersect(c.List).Any(). We will talk about doing this, but this is a lot of work and would be difficult to document. | ||||||
| Comment by Gautier [ 09/May/12 ] | ||||||
|
Thanks for your reactiveness http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in Maybe this can be implemented in the Intersect linq method. Not really a "Contains" method, but close. | ||||||
| Comment by Craig Wilson [ 06/May/12 ] | ||||||
|
Thanks for your enthusiasm. This will be implemented for release 1.5 and will support both the Enumerable.Contains extension method invoked upon a local constant as well as the Contains method of ICollection<T> invoked upon a local constant. Let me know if there are any more "Contains" methods that should be utilized. | ||||||
| Comment by Brian [ 06/May/12 ] | ||||||
|
There is an other problem - when you update mongo driver from 1.3 version up to the 1.4 version all your queries with "Contains" must be verified. There would not any compile-time exceptions. Just your query will fail in run-time. That was heppened with my project. I spend some time to check each query because in my DAL are two types of queries: mongo queries and linq-to-object, so i must check where I shoud set "In" and where must be "Contains". | ||||||
| Comment by Alexander Nagy [ 05/May/12 ] | ||||||
|
+1 for Contains over In; Contains is a standard part of .Net via ICollection<T> whereas In is not. | ||||||
| Comment by Gautier [ 04/May/12 ] | ||||||
|
And to use .In() (like other Linq To MongoDb extension methods), It is necessary to reference the MongoDb Driver dll outside of the database access layer. | ||||||
| Comment by Ryan Hoffman [ 04/May/12 ] | ||||||
|
@Robert - I think the driver should support either way (or, if anything using .Contains() and not .In). All other LINQ providers that I have used (Objects, SQL, EF, and NHibernate) have done "in" queries with .Contains(). I have just converted several NHibernate LINQ queries to MongoDB LINQ queries, and this is one inconsistency. It is not a big deal to change it to use the .In extension method provided by the driver, however I believe this should be done consistently with other LINQ implementations. | ||||||
| Comment by Brian [ 03/May/12 ] | ||||||
|
I'm sorry because of my bug is that only that I don't know how use that library properly. | ||||||
| Comment by Robert Stam [ 03/May/12 ] | ||||||
|
Have you tried:
Perhaps we should support either way? | ||||||
| Comment by Brian [ 03/May/12 ] | ||||||
|
FluentMongo supported this |