這是我正在嘗試做的。有一個用空格分隔的數字序列。保存這些數字之後,即使數字在序列中出現了n次,我也只想返回一次所有數字的字符串。使用Linq只返回一個項目一次
string[] tempNumbers = textValue.Split(' ');
IEnumerable<string> distinctNumbers = tempNumbers.Where(value => value.Distinct());
我得到這個錯誤:
Error 2 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'bool' c:\users\sergio\documents\visual studio 2010\Projects\LinqPlayground\LinqPlayground\SetSemanticsExample.cs 67 75 LinqPlayground
我這樣做,但我不明白的IEnumarable的值,只有類型名被放置在文本框。 txtResult.Text = distinctNumbers.ToList()。ToString(); – 2010-07-11 23:21:58
@Sergio Tapia:這是因爲List的ToString方法只顯示類型名稱,而不是列表的內容。看到我的更新回答。 – 2010-07-11 23:24:23