Manual constructed expressions problem

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: 2.0.1
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

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

              Created:
              Updated:
              Resolved: