I have a sample CSV file as follows
- 1,A
- 2,B
- 3,C
Code:
var query = File.ReadAllLines("test.txt")
.Select(record => record.Split(','))
.Select(tokens => new { clearNum = tokens[0], MPID = tokens[1] });
foreach (var item in query)
{
Console.WriteLine("{0}, {1}", item.clearNum, item.MPID);
}
I am able to print the items.
I need to send the output of LINQ query to LIST
public class icSASList
{
public string ClearNum { get; set; }
public string MPID { get; set; }
}
List clearList = new List;
After considering the accepted answer, I’d like to suggest a solution that requires less object initializations. If the list is large, this will make a difference.
Oh, yeah, using
record.Split(',')is naive – it’s normally allowed to have commas in ” (quoted) fields, which will break your program. Better use something like http://www.filehelpers.com/.