c# Add a Column to end of CSV file

后端 未结 4 1153
深忆病人
深忆病人 2020-12-20 22:05

Trying to add a column to the end of a cvs file, which will just count up the number of lines (or can all be the same number it doesn\'t really mater) as the main aim is to

4条回答
  •  执笔经年
    2020-12-20 22:30

    Without examining much more, I can tell you that you definitely have a challenge with the lambda you're using for the update:

        //add new column value for each row.
        lines.Skip(1).ToList().ForEach(line =>
        {
            //-1 for header
            lines[index] += "," + newColumnData[index - 1];
            index++;
        });
    

    This would make more sense to me:

        //add new column value for each row.
        lines.Skip(1).ToList().ForEach(line =>
        {
            line += "," + newColumnData[++index - 2];
        });
    

    The lines[index] part doesn't make sense in a ForEach since you're looping through each line separately.

提交回复
热议问题