Accessing List elements by name instead of by number...
-
Saturday, September 22, 2012 12:49 AM
// open the file "data.csv" which is a CSV file with headers using (CsvReader csv = new CsvReader(new StreamReader(textBox1.Text), true)) { int fieldCount = csv.FieldCount; string[] headers = csv.GetFieldHeaders(); while (csv.ReadNextRecord()) { List<string> list = new List<string>(); for (int i = 0; i < fieldCount; i++) { list.Add(csv[i].ToString()); } lInventory.Add(list); } }Right now I am using Add to cycle through each field. There is an array 'headers' which is loaded with the headers in string form. It might contain values like "QTY","TITLE","DESCRIPTION". I would like to be able to access list more like 'list["QTY"]' or perhaps more like lInventory[0]["QTY"]. I have the structure set up but I'm just not at at all sure how to make the leap from this point to where I would like be with names.
It is probably worth mentioning that csv[i] and headers[i] would be the same column.
Thanks in advance!
- Edited by S.e.p.y Saturday, September 22, 2012 12:50 AM
All Replies
-
Saturday, September 22, 2012 1:12 AM
-
Saturday, September 22, 2012 1:37 AMModerator
if you are using this: http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=9258
looking at the source, looks like it implements an indexer off of the field name -
try -
using (CsvReader csv = new CsvReader(new StreamReader(textBox1.Text), true)) { string[] headers = csv.GetFieldHeaders(); while (csv.ReadNextRecord()) { lInventory.Add( headers .Select(fieldName => csv[fieldName]) .ToList() ); } }gimme some slamming techno!!!!
- Proposed As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Tuesday, September 25, 2012 3:42 AM
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Tuesday, September 25, 2012 3:43 AM
-
Saturday, September 22, 2012 2:11 AMThat is precisely what I am using. I visited your link and saw the name of Mr. Lorion there. That's very interesting how you are using that code. I am going to have to try it out! Thank You!
-
Saturday, September 22, 2012 2:13 AMModeratornot my code. . just binged CsvReader and looked at the source
gimme some slamming techno!!!!
-
Saturday, September 22, 2012 2:41 AMThere does not seem to be a .select underneath headers...
-
Saturday, September 22, 2012 2:44 AM
using System.IO; using LumenWorks.Framework.IO.Csv; void ReadCsv() { // open the file "data.csv" which is a CSV file with headers using (CsvReader csv = new CsvReader( new StreamReader("data.csv"), true)) { myDataRepeater.DataSource = csv; myDataRepeater.DataBind(); } }
There is this sample but I am confused about how a DataTable might apply to this. -
Saturday, September 22, 2012 3:41 AMModerator
Always add
using System.Linq;
to the list of your using statements.
gimme some slamming techno!!!!
-
Saturday, September 22, 2012 3:46 AMThe target for my project has to be .NET 2.0. Does Linq exist there?
-
Saturday, September 22, 2012 5:43 AMModerator
Ack!!! Why in the world would you be targeting framework 2.0?????????????
using (CsvReader csv = new CsvReader(new StreamReader(textBox1.Text), true)) { string[] headers = csv.GetFieldHeaders(); while (csv.ReadNextRecord()) { List<string> data = new List<string>(); foreach(string header in headers) { data.Add(csv[header]); ); lInventory.Add(data); } }gimme some slamming techno!!!!
-
Saturday, September 22, 2012 8:27 AM
It's because of eBay... I really appreciate you sharing your code with me...

