My .csv is in the format –
Group No,Group Name,Type,Class,Category,Description,Product,Quantity,Stock,Id,Name
I am trying to read this .csv using the following LINQ –
var data = lines
.Select((line, index) => new { Line = line, Index = index + 1 }) // capture line number
.Skip(1) // skip header
.Select(x => x.Line.Split(',').Select(field => field.Trim()).ToArray()
.Select(fields => new {
grpNo = fields[0],
product = fields[6],
Qty = fields[7],
stock = fields[8],
ID = fields[9],
Name = fields[10],
LineIndex = x.Index
})).ToList();
The issue is that this LINQ is not giving me any results. I debugged this LINQ step by step so I found out that till this portion my LINQ is working fine and I can see the data –
var data = lines
.Select((line, index) => new { Line = line, Index = index + 1 }) // capture line number
.Skip(1) // skip header
.Select(x => x.Line.Split(',').Select(field => field.Trim()).ToArray()
But when I include the following portion –
Select(fields => new {
grpNo = fields[0],
product = fields[6],
Qty = fields[7],
stock = fields[8],
ID = fields[9],
Name = fields[10],
LineIndex = x.Index
})).ToList();
it gives me nothing. Upon debugging, I found out that in the data variable, it says, index out of bounds of the array
But I rechecked it and I know I am using proper index numbers for every field.
I even removed everything and checked for grpNo = fields[0]
, it gave me the same message “Index out of Bounds”.
Please suggest. Thank you!