Accepted answer

or if you want to use the c# language extensions:

var words = (from line in new[] { "one two", "three four five" }
             from word in line.split(' ', stringsplitoptions.removeemptyentries)
             select word).count();


not an answer to the question (that was to use linq to get the combined word count in the array), but to add related information, you can use strings.split and strings.join to do the same:


string[] stringarray = { "one two", "three four five" }; 
int numwords = strings.split(strings.join(stringarray)).length;

    dim stringarray() as string = {"one two", "three four five"}
    dim numwords as integer = split(join(stringarray)).length


i think sum is more readable:

var list = new string[] { "1", "2", "3 4 5" };
var count = list.sum(words => words.split().length);


you can do it with selectmany:

var stringarray = new[] {"one two", "three four five"};
var numwords = stringarray.selectmany(segment => segment.split(' ')).count();

selectmany flattens the resulting sequences into one sequence, and then it projects a whitespace split for each item of the string array...

Related Query