forum

Home / DeveloperSection / Forums / Casting Ado.net DataReader to IDataRecord giving strange result

Casting Ado.net DataReader to IDataRecord giving strange result

Madam Walker392519-Jun-2013
Hi Expert, 

I have a query that I run against the database, and I can see that there is a record for 31/05/2013. When I run this query from C# with ADO.NET, and then use the following code, I am missing the record for 31/05/2013

var timeSeriesList = new List<TimeSeries>();  
using (var reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        timeSeriesList = reader.Cast<IDataRecord>()
            .Select(r => new TimeSeries
                 {
                     MidRate = (double)r["MidRate"],
                     RiskFactorName = (string)r["RiskFactorName"],
                     SeriesDate = (DateTime)r["SeriesDate"]
                 }).ToList();
    }
}

However, if I use the same query with this code:

var timeSeriesList = new List<TimeSeries>();                        
using (var reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        var timeSeries = new TimeSeries
                 {
                     MidRate = (double)reader["MidRate"],
                     RiskFactorName = (string)reader["RiskFactorName"],
                     SeriesDate = (DateTime)reader["SeriesDate"]
                 };
        timeSeriesList.Add(timeSeries);
    }
}

...then the record at 31/05/2013 is in the collection - why would the first block of code give this result?


Updated on 19-Jun-2013

Can you answer this question?


Answer

1 Answers

Liked By