0

Im reading csv files into a list. since each file contains many lines and time-consuming to read, I only need to check the first 100 lines for each file. How can I have Linq to only read first 100 lines and then continue to the next file?

For(i=0;i<10;i++)
     line_list = (from line in File.ReadLines(files[i])
                  let field = line.Split(',')
                  where field[2] == "Apple" && !field[3].Contains("Banana")
                  select field).ToList();

3 Answers 3

5

You can use Enumerable.Take to take only the first 100 lines:

var first100Lines = File.ReadLines(path).Take(100);

for what it's worth, here the all in one:

List<List<string[]>> allFileLineColumns = files
    .Select(fPath => 
        File.ReadLines(fPath)
            .Take(100)
            .Select(l => new { Line=l, Cols=l.Split(',') })
            .Where(x => x.Cols.Length >= 4 && x.Cols[2] == "Apple" && !x.Cols[3].Contains("Banana"))
            .Select(x => x.Cols)
            .ToList()
    ).ToList();

Although i would split it into multiple parts to increase readability.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to concat the result into line_list

          for(i=0; i<10; i++)
          {
              var fieldsList = from line in File.ReadLines(files[i])
                               let field = line.Split(',')
                               where field[2] == "Apple" && !field[3].Contains("Banana")
                               select field;

                line_list = line_list.Concat(fieldsList.Take(100).ToList()).ToList();
          }

Comments

0

This is a weird use case for linq, but try take()...

For(i=0;i<10;i++)
     line_list = (from line in File.ReadLines(files[i]).Take(100)
                  let field = line.Split(',')
                  where field[2] == "Apple" && !field[3].Contains("Banana")
                  select field).ToList();

1 Comment

The Take should be on ReadLines as in Tims example. Otherwise it processes all the lines anyway and then Takes 100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.