tags:

views:

172

answers:

2

Hi,

Using LINQ, what is an efficent way to get each string from a tab-delimited .txt file (and then get each word, usually what string.Split(...) does)?

var v = from line in File.ReadAllLines()
   select n

Is part of this solution I believe. I don't mind if this uses yield return.

EDIT: I've also seen threads on here detailing exactly what I am trying to do, but can't find them.

A: 

Using File.ReadAllLines is easy - but not necessarily the most efficient, since it reads the entire line into memory.

A short version would probably be:

var wordsPerLine = from line in File.ReadAllLines(filename)
               select string.Split(line, '\t');

foreach(var line in wordsPerLine)
{
    foreach(word in line)
    {
        // process word...
    }
}

If you want a single enumerable of the words, you can use SelectMany to get that, too...

Reed Copsey
Definitely not the shortest version. Query operators are too verbose ;) `File.ReadAllLines("file.txt").Select(line => line.Split('\t'))`
Mehrdad Afshari
Yeah - but the OP liked using query operators, so I left it.
Reed Copsey
+3  A: 

I'm not entirely sure what you're asking but it sounds like you're trying to get every word from a tab delimited file as an IEnumerable<string>. If so then try the following

var query = File.ReadAllLines(somePathVariable)
                .SelectMany(x => x.Split(new char[] { '\t' });
JaredPar
+1 great answer and use of Lambda Expressions
masenkablast
That's what I'm looking for, apologies for not wording my post well, wasn't sure how best to explain the issue. How could I combine that with yield return to return every word in a string line?
dotnetdev