any help on this scenario please.. new to c#.
Summary:
- Need to read the contents of csv into array.
- Skip first line of array, then sort on column 1 (company)
- Then use sorted array (without header if easier) further along in code.
Example csv:
Company,Item,Vendor
Comp1,Bulb,BnQ
Comp2,Plug,Tesco
Comp1,Kettle,Wickes
This is my attempt but I get the following error when attempting to access sorted[j] and I cant work out a way around this.
Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable
Code:
var csvRows = System.IO.File.ReadAllLines(filename).Skip(1);
var sorted = csvRows.Select(line => new
{
SortKey = Int32.Parse(line.Split(',')[0]),
Line = line
})
.OrderBy(x => x.SortKey)
.Select(x => x.Line);
int maxCount = sorted.Count();
for (int j = 0; j < maxCount; j++)
{
string line1 = sorted[j].ToString();
var values = line1.Split(',');
...
.ToList()will work:.Select(x => x.Line).ToList();, otherwise you canforeachinstead offor:foreach (var line in sorted) { var line1 = line.ToString(); ...Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerabledirectly into Google tells you how to fix it.