[CSHARP-1696] Object must implement IConvertible Created: 27/Jun/16  Updated: 02/Feb/22  Resolved: 02/Feb/22

Status: Closed
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.2.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Marko Hrovatic Assignee: Unassigned
Resolution: Done Votes: 0
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows


Epic Link: CSHARP-3615

 Description   

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).



 Comments   
Comment by James Kovacs [ 02/Feb/22 ]

We were not able to reproduce this issue with recent versions of the driver. If you are still observing this issue, please re-open this ticket with any updated information that you can provide.

Comment by Marko Hrovatic [ 27/Jun/16 ]

seriously, cannnot edit the Description?

Ok here goes

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).

Generated at Wed Feb 07 21:40:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.