[CSHARP-4542] LINQ3 support for projecting to a ValueTuple<> Created: 22/Feb/23  Updated: 09/Mar/23  Resolved: 09/Mar/23

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.19.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Damith G Assignee: Robert Stam
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-4524 Handle projections using constructors... Closed
Related
is related to CSHARP-4564 Write a custom serializer for ValueTu... Closed
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   

the following code results in an expression not supported exception in LINQ3 while it used to work with LINQ2:

var result = await personQueryable
    .Select(p => new ValueTuple<string, string>(p.FirstName, p.LastName))
    .ToListAsync(); 

stack trace:

MongoDB.Driver.Linq.ExpressionNotSupportedException
  HResult=0x80131515
  Message=Expression not supported: new ValueTuple`2(p.FirstName, p.LastName) because constructor parameter item1 does not match any property.
  Source=MongoDB.Driver
  StackTrace:
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.NewExpressionToAggregationExpressionTranslator.GetMatchingPropertyName(NewExpression expression, String constructorParameterName)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.NewExpressionToAggregationExpressionTranslator.<>c__DisplayClass0_0.<Translate>b__1(ParameterInfo p)
   at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.NewExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, NewExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.Translate(TranslationContext context, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.ExpressionToAggregationExpressionTranslator.TranslateLambdaBody(TranslationContext context, LambdaExpression lambdaExpression, IBsonSerializer parameterSerializer, Boolean asRoot)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.SelectMethodToPipelineTranslator.Translate(TranslationContext context, MethodCallExpression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators.ExpressionToPipelineTranslator.Translate(TranslationContext context, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExpressionToExecutableQueryTranslator.Translate[TDocument,TOutput](MongoQueryProvider`1 provider, Expression expression)
   at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.ToCursorAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.<ToListAsync>d__16`1.MoveNext()

however projecting to a Tuple<> works just fine.



 Comments   
Comment by Robert Stam [ 09/Mar/23 ]

I have verified that CSHARP-4524 allows ValueTuples to be serialized using the BsonClassMapSerializer.

Note however that once CSHARP-4564 is implemented ValueTuples will be serialized using custom serializers that will serialize ValueTuples as arrays the same way as Tuples are.

Comment by Robert Stam [ 23/Feb/23 ]

The code review is of the reproduction and the proof that CSHARP-4524 fixed the issue.

There are no driver changes involved.

Comment by Robert Stam [ 23/Feb/23 ]

The difference between `Tuple` and `ValueTuple` is that `Tuple` has properties called `Item1` etc and `ValueTuple` has fields called `Item1` etc.

That means that this is actually a duplicate issue of CSHARP-4524. I have verified that once the fix for CSHARP-4524 is in place that projecting to `ValueTuple` now works.

Comment by Robert Stam [ 22/Feb/23 ]

Thanks for reporting this issue. We can definitely support this. I will look into it soon

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