[英]How to write a Lambda Expression to select distinct words from list of sentences
如何編寫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
var distinctWords = famousquotes
.SelectMany(s =>
s.Split(new []{' '},
StringSplitOptions.RemoveEmptyEntries))
.Distinct();
使用SelectMany獲取所有基礎字符串。 然后使用Distinct完成您想要的操作。
沒什么花哨的。一個非常簡單的技巧:)
var result = String.Join(" ",famousquotes).Split(' ').Distinct();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.