none
How to change the Headline using Linq RRS feed

  • Question

  • Hi Guys I have this code and I use it to read files and then put them as strings into a IEnumerable.
    I just have the problem that all first rows should be HeadLines but in my LineNumber column im getting 1 as the name is there a way to change that and write "RowNumber" instead?

    var lines = File.ReadLines(readFolderFile, Encoding.Default)   
       .Select((line, index) => new { Line = line.Trim(';'), LineNumber = index + 1 })  
       .Where(x => !String.IsNullOrWhiteSpace(x.Line))  
       .Select(x => $"{x.LineNumber};{x.Line};");  



    • Edited by bkreye Saturday, September 8, 2018 10:01 AM
    Saturday, September 8, 2018 9:59 AM

All replies

  • Hi bkreye,

    Please try the following LINQ. 

    var lines = File.ReadLines(readFolderFile, Encoding.Default)
                               .Select((line, index) => new { Line = line.Trim(';'), LineNumber = index.ToString()  })
                               .Where(x => !String.IsNullOrWhiteSpace(x.Line))
                               .Select(x => string.Format("{0};{1};", x.LineNumber == "0" ? "RowNumber": x.LineNumber, x.Line)).ToList();

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 10, 2018 3:31 AM
    Moderator