Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-5240

Exception when using Linq for property assignment in Set stage

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.28.0
    • Component/s: LINQ3
    • None
    • Dotnet Drivers

      Summary

      When using Linq to assign a property in a Set stage, this works only under certain conditions. Otherwise, an exception is raised.

      The code (see below) works only as lonq as the type of the SubDocuments property is of type List<SubDocument>. A change to IList<T>, IEnumerable<T>, HashSet<T> along with adjusting the Linq statement (e.g. removing ToList()) leads to the following exception:

      MongoDB.Driver.Linq.ExpressionNotSupportedException: 'Expression not supported: x.SubDocuments.Where(y => (y.PartnerId == "123")).ToList() because member and value serializers are not compatible.'
      

      The exception is raised here

      Also see this answer on Stackoverflow.

       

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

      The sample works for List<T> with version 2.28.0; changing the property type or downgrading to 2,26.0 (and keeping List<T>) leads to the exception.

      How to Reproduce

      public class Document 
      {
          public List<SubDocument> SubDocuments { get; set; } = new();
      }
      
      public class SubDocument
      {
          public string Id { get; set; } = null!;
          public string PartnerId { get; set; } = null!;
      } 
      
      var coll = db.GetCollection<Document>("docs");
      var pipeline = new EmptyPipelineDefinition<Document>()
          .Set(x => new Document()
          {
              SubDocuments = x.SubDocuments
                .Where(y => y.PartnerId == "123").ToList(),
          });
      var result = await (await coll.AggregateAsync(pipeline)).ToListAsync();
      

      Additional Background

      Appending a $set-stage that is taken from a BsonDocument can be used as a work-around.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            m.wildgruber@sevantage.de Markus Wildgruber
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: