c# Add a Column to end of CSV file

后端 未结 4 1159
深忆病人
深忆病人 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:13

    You should not index inside the foreach. The foreach gives you a line at a time.

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

    The lambda expression means: take each element in the list, and call it "line", and do what is inside the curly brackets to it.

    Also, as I see it here, your newColumnData only seems to have one item in it, the string "D". Yet you are indexing it as if there was one item in this list for each line in the csv file you read. That too will cause an index out of range if your csv file has more than one line in it but... never mind, the more I think about it, the more you should just go with Dmitry Bychenko's answer.

提交回复
热议问题