Ondrej Slinták Ondrej Slinták - 1 year ago 53
C# Question

Read text data from file using LINQ

I have following text file:

37 44 60
67 15 94
45 02 44

How to read all numbers from this file and save them into two-dimensional array, using LINQ? All I manged to do was creating a simple array with all first values in each row. Is using LINQ in this case a good idea or should I simply load the file normal way and parse it?

Answer Source
    .Select(l => l.Split(' ').Select(int.Parse).ToArray()).ToArray();


List<int[]> forThoseWhoHave1GigFiles = new List<int[]>();
using(StreamReader reader = File.OpenText(myFile))
        string line = reader.ReadLine();
        forThoseWhoHave1GigFiles.Add(line.Split(' ')
var myArray = forThoseWhoHave1GigFiles.ToArray();


    .Select(l => l.Split(' ')

In .Net 4.0 and above.