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

LINQ3: support calls to constructors in Select

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 2.16.1
    • Affects Version/s: None
    • Component/s: LINQ3
    • None

      Support expressions like: 

      queryable().Select(_ => new KeyValuePair(...))
      

      Note: after investigation I'm modifying the description of this ticket to make it more general to refer to the use of any constructor call in Select statement. The current implementation assumes that any `NewExpression` is the creation of an anonymous type value, but that's not always true.

      The following test case illustrates a simple case of the more general problem to be solved:

      [Fact]
      public void Select_with_constructor_call_should_work()
      {
          var client = DriverTestConfiguration.Linq3Client;
          var database = client.GetDatabase("test");
          var collection = database.GetCollection<C>("test");
      
          var queryable = collection.AsQueryable()
              .Select(c => new D(c.X));
      
          var stages = Linq3TestHelpers.Translate(collection, queryable);
          var expectedStages = new[]
          {
              "{ $project : { Y : '$X', _id : 0 } }"
          };
          Linq3TestHelpers.AssertStages(stages, expectedStages);
      }
      
      private class C
      {
          public int Id { get; set; }
          public int X { get; set; }
      }
      
      private class D
      {
          public D(int y)
          {
              Y = y;
          }
      
          public int Y { get; set; }
      }
      
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            boris.dogadov@mongodb.com Boris Dogadov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: