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

Manual constructed expressions problem

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.0.1
    • None
    • None

    Description

      I have a simple "Event" class with a DateTime "Time" property.
      Let "eventColelction" be IMongoCollection<Event>

      If I create an index using the following code

      Task.Run(() => eventCollection.Indexes.CreateOneAsync(Builders<Event>.IndexKeys.Ascending(_ => _.Time))).Wait();
      

      everything works fine.

      Then, I constructed expression by hand:

      var arg = Expression.Parameter(typeof(Event), "arg");
                  var getter = Expression.Property(arg, typeof (Event).GetProperty("Time"));
                  var convert = Expression.Convert(getter, typeof(object));
                  var exp2 = Expression.Lambda<Func<Event, object>>(convert, arg);
      Task.Run(() => eventCollection.Indexes.CreateOneAsync(Builders<Event>.IndexKeys.Ascending(exp2))).Wait();
      

      it worked again.

      Then I ported the code to F# (it was my original goal):

      let arg = Expression.Parameter(typeof<Event>, "arg")
      let getter = Expression.Property(arg, typeof<Event>.GetProperty("Time"))
      let convert = Expression.Convert(getter, typeof<obj>);
      let exp = Expression.Lambda<Func<Event, obj>>(convert, arg);
       
      let createIndex keyDefinition =
              eventCollection.Indexes.CreateOneAsync(keyDefinition) |> Async.AwaitTask |> Async.RunSynchronously |> ignore
       
      createIndex (Builders<Event>.IndexKeys.Ascending(exp))
      

      And it ended with "System.InvalidOperationException: Unable to determine the serialization information for arg => Convert(arg.Time)."

      The question is why? Expression are just identical.

      Attachments

        Activity

          People

            Unassigned Unassigned
            danslapman Daniel Slapman
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: