I'm trying to return an HTTP Response that consists of an array of records I've selected from a database. But I'm having trouble mapping my IDataReader
to an Enumerable
. Here's the part of my code that's relevant:
namespace Web.Api.Controllers
{
public static class Blah
{
public static IEnumerable<T> Select<T>(this IDataReader reader,
Func<IDataReader, T> projection)
{
while (reader.Read())
{
yield return projection(reader);
}
}
}
[HttpPost]
[Route("Search")]
public async Task<Tuple<int, IEnumerable<CustomerViewModel>, int>> Search([FromBody]CustomerViewModel model)
{
var s = _configuration.GetConnectionString("DefaultConnectionAlt");
using (SqlConnection connection = new SqlConnection(s))
{
connection.Open();
using (SqlCommand command = new SqlCommand("Search @RegistrationDate=\"2020-07-09\"", connection))
{
using (IDataReader reader = command.ExecuteReader())
{
var results = reader.Select<CustomerViewModel>(CustomerViewModel.Create);
return Tuple.Create(0, results, 29);
}
}
}
}
}
When I send a POST request to http://localhost:42432/api/Search
, the line while (reader.Read())
gives the error:
System.InvalidOperationException: 'Invalid attempt to call Read when reader is closed.'
I put a breakpoint at return Tuple.Create(0, results, 29);
, and when I inspect the results
variable, it shows the results I expect. But after I step out of that breakpoint, I get the error on the while (reader.Read())
.
Can someone tell me how I can fix my problem?
I was following examples listed here:
How can I easily convert DataReader to List<T>?
Convert rows from a data reader into typed results
EDIT - I am using dotnetcore