SelectMany second parameter problem

  • Thread starter Thread starter Edward
  • Start date Start date
E

Edward

Say I have an array of poems and I want to get all words in these poems.

Code:
string[] poems = { "Birds are singing.", "Ducks are playing." };
var voc = poems.SelectMany
(poem => poem.Split(' ', '.'), (word) => word.Length > 0)
In the second lambda parameter I want to exclude those empty strings
created by the first lambda Split expression. But the compiler thinks
the second parameter is not of correct type.

How can I write the second lambda expression?
 
You just need to apply a "where" to the selection, as below (only the
first bit is needed - the rest is just to demo)

string[] poems = { "Birds are singing.", "Ducks are
playing." };
var voc = poems.SelectMany(
poem => poem.Split(' ', '.')
.Where(word => word.Length > 0));

foreach(var word in from word in voc
group word by word into agg
orderby agg.Key
select agg)
{
System.Console.WriteLine("{0}: {1}",
word.Key, word.Count());
}

Marc
 
Back
Top