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

Object must implement IConvertible

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.2.4
    • Component/s: Linq
    • Environment:
      windows

      With this query handler

      public class AppointmentsFromDateToDateQuery : IQuery<IEnumerable<Appointment>>
      {
      public DateTimeOffset From
      { get; set; }
      public DateTimeOffset To { get; set; }
      }
      public class AppointmentsFromDateToDateQueryHandler : IQueryHandlerAsync<AppointmentsFromDateToDateQuery, IEnumerable<Appointment>>
      {
      private IMongoDatabase _db;
      public AppointmentsFromDateToDateQueryHandler(IMongoDatabase db)
      { _db = db; }
      public async Task<IEnumerable<Appointment>> HandleAsync(AppointmentsFromDateToDateQuery query)
      { return await _db.GetCollection<Appointment>(typeof(Appointment).Name) .Find(a => a.Starts >= query.From && a.Ends <= query.To) .SortBy(a => a.Starts) .ToListAsync(); }
      and this usage
      var query = new AppointmentsFromDateToDateQuery()
      { From = date.Value, To = date.Value.AddDays(1) }
      ;
      var items = await new AppointmentsFromDateToDateQueryHandler(_db).HandleAsync(query);
      

      the driver would throw an exception

      [InvalidCastException: Object must implement IConvertible.]
      System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +14092601
      MongoDB.Driver.Linq.Expressions.ISerializationExpressionExtensions.ConvertIfNecessary(Type targetType, Object value) +149
      MongoDB.Driver.Linq.Expressions.ISerializationExpressionExtensions.SerializeValue(ISerializationExpression field, Object value) +112
      MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateComparison(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression) +283
      MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) +461
      MongoDB.Driver.Linq.Translators.PredicateTranslator.TranslateAndAlso(BinaryExpression node) +82
      MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) +283
      MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) +76
      MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation(FilterDefinition`1 filter, FindOptions`2 options) +358
      MongoDB.Driver.MongoCollectionImpl`1.FindAsync(FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken) +174
      MongoDB.Driver.FindFluent`2.ToCursorAsync(CancellationToken cancellationToken) +125
      MongoDB.Driver.<ToListAsync>d__16`1.MoveNext() +134
      System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
      System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
      System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +31
      Roswell.Plugins.Customers.Queries.<HandleAsync>d__2.MoveNext() +2923
      System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13891908
      System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61
      System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +31
      

      Only and only when the collection in question would be empty.
      The problem actually lies in the return type of IEnumerable which the driver apparently has a trouble of instantiating.
      This is something that normally works with Entity Framework (returning empty IEnumerables).

            Assignee:
            Unassigned Unassigned
            Reporter:
            brgrz Marko Hrovatic
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: