[CSHARP-4772] Add support for List<T>.Exists and Array.Exists<T> in LINQ3 Created: 24/Aug/23  Updated: 28/Oct/23  Resolved: 14/Sep/23

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 2.21.0
Fix Version/s: 2.22.0

Type: Bug Priority: Minor - P4
Reporter: Sasan Pasha Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
backported by CSHARP-4743 Add support for DateTime.Date and Dat... Closed
Duplicate
is duplicated by CSHARP-4773 Linq3Implementation: MongoDB.Driver.L... Closed
Documentation Changes: Not Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

The use of  Linq "Exists" in the query is not supported.

MongoDB.Driver.Linq.ExpressionNotSupportedException: Expression not supported: p.OrganizationIds.Exists(o => (o == "045d9fd6-677d-4b9f-a288-4478e75c2da4")).
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodCallExpressionToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.MethodCallExpression expression) [0x01419] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.Expression expression) [0x00133] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators.AnyMethodToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.MethodCallExpression expression) [0x000a4] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodCallExpressionToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.MethodCallExpression expression) [0x011f3] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.Expression expression) [0x00133] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.BinaryExpressionToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.BinaryExpression expression) [0x000c2] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.Expression expression) [0x0010c] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateUsingAggregationOperators (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.Expression expression) [0x00000] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.Expression expression, System.Boolean exprOk) [0x0000b] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ExpressionToFilterTranslator.TranslateLambda (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.LambdaExpression lambdaExpression, MongoDB.Bson.Serialization.IBsonSerializer parameterSerializer, System.Boolean asRoot) [0x00093] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.WhereMethodToPipelineTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.MethodCallExpression expression) [0x0003e] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate (MongoDB.Driver.Linq.Linq3Implementation.Translators.TranslationContext context, System.Linq.Expressions.Expression expression) [0x003a1] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput] (MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1[TDocument] provider, System.Linq.Expressions.Expression expression) [0x00015] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2[TDocument,TOutput].ToCursorAsync (System.Threading.CancellationToken cancellationToken) [0x00000] in <23bc2c72d0e04efa9278964cb99d6993>:0 
  at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument] (MongoDB.Driver.IAsyncCursorSource`1[TDocument] source, System.Threading.CancellationToken cancellationToken) [0x0000e] in <b1986f4c9c644071ac801bfae5975e5e>:0 
  at Infrastructure.Database.CosmoMongoDb.GetAccountsByOrganizationIdAsync (System.String organizationId) [0x001a7] in

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

How to Reproduce

Steps to reproduce. If possible, please include a Short, Self Contained, Correct (Compilable), Example.

        var accounts = await AccountsCollection.AsQueryable().Where(a =>
                a.Profiles != null &&
                a.Profiles.Exists(p => p.OrganizationIds.Exists(o => o == organizationId))).ToListAsync()

 

 

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Githook User [ 14/Sep/23 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4772: Add support for List<T>.Exists and Array.Exists<T> in LINQ3.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7baa925f66e8e34e4665624faa53ccdadab4105a

Comment by Robert Stam [ 31/Aug/23 ]

Thank you for this report. I am able to reproduce the exception you are seeing.

I assume that the `Exists` method you are using is `List<T>.Exists`.

This method is not a "true" LINQ method because it is declared for `List<T>` only and not for all `IEnumerable<T>`. Even so, I think we can easily support it.

Note also that as an immediate workaround you can use `IEnumerable<T>.Any` which is a standard LINQ method that is equivalent to `List<T>.Exists` and is already supported.

For example:

collection.AsQueryable()
    .Where(a =>
        a.Profiles != null &&
        a.Profiles.Any(p => p.OrganizationIds.Any(o => o == organizationId)))
 

Comment by PM Bot [ 24/Aug/23 ]

Hi sasan.pasha@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

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