---
title: "Casting Ado.net DataReader to IDataRecord giving strange result"  
description: "Casting Ado.net DataReader to IDataRecord giving strange result"  
author: "Madam Walker"  
published: 2013-06-19  
updated: 2013-06-19  
canonical: https://www.mindstick.com/forum/1213/casting-ado-dot-net-datareader-to-idatarecord-giving-strange-result  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# Casting Ado.net DataReader to IDataRecord giving strange result

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances), \
I have a [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver) that I run [against](https://yourviews.mindstick.com/view/81332/the-approach-of-science-against-disease-epidemics) the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax), and I can see that there is a record for 31/05/2013. When I run this query from C# with [ADO.NET](https://www.mindstick.com/articles/804/connection-pooling-in-ado-dot-net), and then use the following code, I am [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) 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](https://www.mindstick.com/articles/1527/string-split-in-c-sharp))r["RiskFactorName"],** **SeriesDate = ([DateTime](https://www.mindstick.com/forum/12949/how-to-validate-if-a-datetime-field-is-not-null-empty))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](https://www.mindstick.com/articles/1718/collections-in-java) - why would the first block of code give this [result](https://www.mindstick.com/blog/12011/advantages-of-getting-result-oriented-seo-from-an-agency)?\

## Replies

### Reply by Sumit Kesarwani

Hi,\
I think that you are missing record in first example because you move reader by one and then cast it.\
Try this change and see if it worked:\
**var timeSeries = new List<TimeSeries>();** **using (var reader = cmd.ExecuteReader())****{** **if (reader.HasRows)** **{** **timeSeries = reader.Cast<IDataRecord>()** **.Select(r => new TimeSeries** **{** **MidRate = (double)r["MidRate"],** **RiskFactorName = (string)r["RiskFactorName"],** **SeriesDate = (DateTime)r["SeriesDate"]** **}).ToList();** **}****}**


---

Original Source: https://www.mindstick.com/forum/1213/casting-ado-dot-net-datareader-to-idatarecord-giving-strange-result

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
