0

I have the following Code so far:

var lines = from rawLine in File.ReadLines(readFolderFile, Encoding.Default) where !string.IsNullOrEmpty(rawLine) && !string.IsNullOrEmpty(rawLine.Trim(';')) select rawLine;

But now I also need in the first "colum" of my IEnumerable the row count. I tried to adapt from other people who had a more or less similar problem but never got it to work so far.

I tried "solutions" like: How do you add an index field to Linq results , Get record's row number using Linq and some other ones. I do unterstand, that I need some kind of index, but I dont understand where do I add my .Select(()=> )

What I tried and obsiously doesnt work is:

var lines = (from rawLine in File.ReadLines(readFolderFile, Encoding.Default) where !string.IsNullOrEmpty(rawLine) && !string.IsNullOrEmpty(rawLine.Trim(';'))
            select rawLine).Select((rawLine, index) =>  index++, rawLine);
Hakunama Tatarov
  • 125
  • 1
  • 14

2 Answers2

2

You indeed need to use Select, and then just to store the index.

var lines = File.ReadLines(readFolderFile, Encoding.Default)
    .Where(x => !string.IsNullOrEmpty(rawLine) && !string.IsNullOrEmpty(rawLine.Trim(';')))
    .Select((x, i) => new
    {
        Line = x,
        Index = i
    }
    .ToList();
Dmitry Korolev
  • 675
  • 4
  • 20
  • 1
    This answer doesn't count empty lines, Tim's does incremet the counter - no idea what OP wants so just mentioned – fubo Sep 06 '18 at 13:11
2

Query syntax doesn't provide the overload of Select (and Where) with the index. So you need to use method syntax.

If you also want to count empty lines but don't include them in the result, select the anonymous type before the Where otherwise after:

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

is there a way, where I could get the result as Line = {Line = "LineNumer;colum1;colum2;columnX..."}

Sure:

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};");
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thank you for your answer I also saw already with my approach I forgot to use the "new" statement but as a result with this or my version I get this: _{ Line = "colum1;colum2;columX...", LineNumber = X } _ is there a way, where I could get the result as Line = _{Line = "LineNumer;colum1;colum2;columnX..."}_ – Hakunama Tatarov Sep 06 '18 at 13:09
  • one more question, is there also the way that I first select only the first row add something like RowNumber as a column name and after that start counting or do I have to it in two steps, with something like `.First()`? – Hakunama Tatarov Sep 06 '18 at 13:22
  • @HakunamaTatarov: sorry, i dont' understand your question – Tim Schmelter Sep 06 '18 at 13:31
  • no problem, I try to explain it a bit different. right now with the last solution you get as the column name "1" because it starts couting from line 1. but instead of 1 im trying to write RowNumber. Because the first row in every file I read include the column names. – Hakunama Tatarov Sep 06 '18 at 14:00
  • @HakunamaTatarov: well, `$"RowNumber{x.LineNumber};{x.Line};"` – Tim Schmelter Sep 06 '18 at 14:05
  • this is what I got so far ` var lines = ..... .Select(x => $"RowNumber;{x.Line};").First(); var lines2 = ..... .Select(x => $"{x.LineNumber};{x.Line};").Skip(1);` And then I guess there is some method to concat both IE. – Hakunama Tatarov Sep 06 '18 at 14:26
  • @HakunamaTatarov: why `First`? You don't need to concat the first and the rest – Tim Schmelter Sep 06 '18 at 14:27
  • hm I cant really follow this syntax then. x.LineNumber is the LineNumber of the "dataset" x and x.Line is the information of this "dataset". If I understand this correct. But Im not really a developer or still learning, I dont see how to manipulate the term `$"RowNumber{x.LineNumber};{x.Line};"` to get what I want, but I'll search a little more, thanks for helping so far :) – Hakunama Tatarov Sep 06 '18 at 14:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179560/discussion-between-tim-schmelter-and-hakunama-tatarov). – Tim Schmelter Sep 06 '18 at 14:41