[CSHARP-3237] NotSupportedException when using ToList on Embedded Documents Created: 23/Oct/20  Updated: 28/Oct/23  Resolved: 15/Nov/22

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: None
Fix Version/s: 2.18.0

Type: New Feature Priority: Major - P3
Reporter: Mohamad Javad Ebrahimi Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

reproduce code

var result = postCollection
	.AsQueryable()
	.Select(p => new
	{
		Id = p.Id,
		Comments = p.Comments.ToList()
	})
	.ToList();

  Message: 
    System.NotSupportedException : The result operation MongoDB.Driver.Linq.Expressions.ResultOperators.ListResultOperator is not supported.
  Stack Trace: 
    AggregateLanguageTranslator.TranslatePipeline(PipelineExpression node)
    AggregateLanguageTranslator.TranslateValue(Expression node)
    AggregateLanguageTranslator.TranslateMapping(ProjectionMapping mapping)
    AggregateLanguageTranslator.TranslateNew(NewExpression node)
    AggregateLanguageTranslator.TranslateValue(Expression node)
    AggregateLanguageTranslator.Translate(Expression node, ExpressionTranslationOptions translationOptions)
    QueryableTranslator.TranslateProjectValue(Expression selector)
    QueryableTranslator.TranslateSelect(SelectExpression node)
    QueryableTranslator.Translate(Expression node)
    QueryableTranslator.TranslatePipeline(PipelineExpression node)
    QueryableTranslator.Translate(Expression node)
    QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions)
    MongoQueryProviderImpl`1.Translate(Expression expression)
    MongoQueryProviderImpl`1.Execute(Expression expression)
    MongoQueryableImpl`2.GetEnumerator()
    List`1.ctor(IEnumerable`1 collection)
    Enumerable.ToList[TSource](IEnumerable`1 source)



 Comments   
Comment by Robert Stam [ 15/Nov/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which was introduced in the 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

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Esha Bhargava [ 09/Nov/20 ]

We are working on a new LINQ provider and we will look at supporting this feature in the context of that project.

Comment by Mohamad Javad Ebrahimi [ 01/Nov/20 ]

Thank you for your workaround but do you have a plan to solve this problem in the future?

Comment by Robert Stam [ 28/Oct/20 ]

A temporary workaround could be to move the call to `ToList` so that it executes client-side:

var result = postCollection
   .AsQueryable()
   .Select(p => new
   {
	   Id = p.Id,
	   Comments = p.Comments
   })
   .AsEnumerable()
   .Select(p => new
   {
	   Id = p.Id,
	   Comments = p.Comments.ToList()
   })
   .ToList();

Everything after `AsEnumerable` runs client-side.

 

Comment by Mohamad Javad Ebrahimi [ 28/Oct/20 ]

Thank you for replying.

Changing type solves this issue but according to our team contracts, we have to use List<T> instead of IEnumerable<T> for these situations.

Comment by Robert Stam [ 26/Oct/20 ]

I can confirm that this is reproducible. We don't support ToList here.

I had to assume some type of Post class to reproduce this. I used:

public class Post
{
	public int Id { get; set; }
	public IEnumerable<string> Comments { get; set; }}

May I ask why you want to call ToList here? Why not just omit it?

var result = postCollection
	.AsQueryable()
	.Select(p => new
	{
		Id = p.Id,
		Comments = p.Comments
	})
	.ToList();

The only difference is the type of the Comments property (IEnumerable<string> instead of List<string>)?

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