简体   繁体   中英

LINQ Groupby C# from CSV

I successfully created a CSV file from a while loop. I ultimately need my code to generate another CSV file that has GroupBy information.

It looks something like this:

while (code)
{
    output3 = split[5].Replace(',', ' ') + ',' +
        split[0].Substring(0, 2) + "-" +
        split[0].Substring(2, 4) + ',' +
        split[4] + ",," + sentprice + ',' +
        split[3] + ',' +
        split[2] + ',' + calccharge + ',' +
        split[1] + ",,," + "NA" + ',' + "A" + ',' +
        split[0].Substring(6, 5) + Environment.NewLine + output3;
}

Now I'm trying to split output3 so that I can group by one of the columns (ie split[1] ).

I've tried:

var table = File.ReadAllLines(FilePath)
    .Select(record => record.Split(','))
    .Select(cell => new { AFPCode = cell[1] }) 
    .GroupBy(x => x.AFPCode);

but that result gives me an index was outside the bounds of the array. As you can see the index is inside the bounds of the array.

I've also tried:

IEnumerable<string> table = 
    from row in File.ReadAllLines(FilePath)
    let elements = row.Split(',')
    let cell = new {AFPCode = elements[1]}
    group p by p.AFPcode into g

That is also not working. Any help will be much appreciated.

Some rows are missing the 2nd column. Did you try filtering out those rows like below?

var table = File.ReadAllLines(FilePath)
    .Select(record => record.Split(','))
    .Where(cell => cell.Length >= 2) // Add filter clause here
    .Select(cell => new { AFPCode = cell[1] }) 
    .GroupBy(x => x.AFPCode);

You have an empty line at the end of your file. That empty line doesn't have a second item when you split it into fields. Either remove the empty line at the end, or filter out the empty line in the code you're using to read in the file.

Some side notes, you shouldn't be constructing large files by using the + operator to concat the strings to each other in a loop. It's a particularly inefficient operation. You can use string.Join to efficiently join a bunch of strings together (either all fields on a line or all lines in a file).

The other option is to write out each line to the file as you compute it (this is actually better still, as you then never have more than one line in memory). This can be done rather easily using File.WriteAllLines if you have a sequence of all of your lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM