如何编写lambda表达式以从以下句子列表中获取不同的单词?
string[] famousquotes =
{
"Advertising is legalized lying",
"Advertising is the greatest art form of the twentieth century"
};
var distinctWords = famousquotes.Select(s => s.Split(' ')).
我希望输出像
Advertising
is
legalized
lying
the
greatest
art
form
of
twentieth
century
答案 0 :(得分:4)
var distinctWords = famousquotes
.SelectMany(s =>
s.Split(new []{' '},
StringSplitOptions.RemoveEmptyEntries))
.Distinct();
使用SelectMany获取所有基础字符串。然后使用Distinct做你想做的事。
答案 1 :(得分:2)
没什么特别的。一个非常简单的伎俩:)
var result = String.Join(" ",famousquotes).Split(' ').Distinct();