Moq.Dapper icon indicating copy to clipboard operation
Moq.Dapper copied to clipboard

ObjectExtensions - ToDataTable does not support string

Open Aaron-Kibbie-ProgLeasing opened this issue 3 years ago • 1 comments

Because a string is an IEnumerable of char when having a result of type string results in: System.AggregateException : One or more errors occurred. (Sequence contains no elements) ----> System.InvalidOperationException : Sequence contains no elements on the internal results variable

I can't create a branch/PR so here is the code change suggested:

static class ObjectExtensions
{
     internal static DataTable ToDataTable(this object result, Type resultType) =>
         (result is not string && result is IEnumerable results) ?
         results.ToDataTable(resultType.GenericTypeArguments.Single()) :
         new[] { result }.ToDataTable(resultType);
}