The Problem:
The Solution:
Code Samples:
DataRow dr = ds.Tables[0].Rows.Cast<DataRow>()
.Where(row => row["Column1"] == DBNull.Value)
.FirstOrDefault();
In this example, I'm wrote a simple Linq query which returned all rows in the DataTable what has any data in the first column. Not the most sophisticated, but you get the point.
What I've Learned:
Originally, I thought casting the DataTable as Enumerable was enough to bring back the desired result. I had a lot of trouble creating a valid where clause, which caused a lot of casting and type comparison errors. Rows.Cast<DataRow> resolved most of the issues. Essentially, casting the collection items to a DataRow type made it easy to grab the values I need quickly.
More Reading:
https://exceldatareader.codeplex.com/
http://stackoverflow.com/questions/12195952/making-a-datarow-select-query
- Couldn't easily read an excel file and query it programmatically.
- Using JSON.Net took a little longer than I thought it would.
- Wanted to brush up on my Linq skills.
- This was a rush job
The Solution:
- Utilized "Excel Data Reader", which allowed me to read Excel data and converted it to a DataSet
- Used Linq to query the data easily, and produced manageable code (for me anyway)
- Used JSON>NET to serialize my code and write to the DOM
Code Samples:
DataRow dr = ds.Tables[0].Rows.Cast<DataRow>()
.Where(row => row["Column1"] == DBNull.Value)
.FirstOrDefault();
In this example, I'm wrote a simple Linq query which returned all rows in the DataTable what has any data in the first column. Not the most sophisticated, but you get the point.
What I've Learned:
Originally, I thought casting the DataTable as Enumerable was enough to bring back the desired result. I had a lot of trouble creating a valid where clause, which caused a lot of casting and type comparison errors. Rows.Cast<DataRow> resolved most of the issues. Essentially, casting the collection items to a DataRow type made it easy to grab the values I need quickly.
More Reading:
https://exceldatareader.codeplex.com/
http://stackoverflow.com/questions/12195952/making-a-datarow-select-query
Comments
Post a Comment