LINQ question with string section and arrays in csv files

Based on the code below:

var query = from line in ClientCount() let aLine = line.Split(",") where aLine.GetValue(1) != 0 select aLine; 

To request the csv file, aLine.GetValue (1) will receive the 1st line (after 0) in the .csv file or the 1st line, for example:

abc, def

And so get def? I want to get def, but then I also want to raise title2 above that in the file, for example:

title, title2

abc, def

How can i do this?

By the way, I know there is LINQ To CSV for this, but I myself do it for practice.

thanks

+4
source share
2 answers

So, if I read correctly, you want to get a pair of "title2", "def"; otherwise put, you want to get the values โ€‹โ€‹of column1. The only change you need for LINQ is as follows:

 var query = from line in ClientCount() let aLine = line.Split(new[] { ',' }) where aLine.GetValue(1) != "0" select aLine.GetValue(1); 

This would exclude rows where the value of the second column is "0", which I assume you were trying to do. This code compiles and runs and gives IEnumerable with the values โ€‹โ€‹"def" and "title2" for the set you specify.

+3
source

While practice - all is well and good, CSV is actually much more complicated than the name suggests - you quoted / did not quit, a multi-line and terrible question, where in some areas โ€œcโ€ becomes a period (โ€œ.โ€).

I highly recommend you use something pre-rolled, like CsvReader .

(Fortunately, TSV is usually not so nasty)

+1
source

Source: https://habr.com/ru/post/1303278/


All Articles