SelectMany second parameter problem

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?
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top