Distinct() 返回字符而不是元素
Distinct() returning char rather than element
我想弄清楚为什么 Distinct() 在这里起作用:
string[] words= new string[] {"Cupcake","Cake","Candy", "Candy"};
var uniqueWords =
from word in words
orderby word
select word;
foreach (var word in uniqueWords.Distinct())
{
Console.Write($" {word}");
但这里没有:
var uniqueWords = from c in array
orderby c
select c.Distinct();
foreach (var element in uniqueWords)
Console.WriteLine($"{element}");
我得到 System.Linq.Enumerable+DistinctIterator`1[System.Char] 控制台输出
我试过转换结果但没有成功。
这是因为你的 select 这里是 select c.Distinct() 就像 select 中字符串 c:
中的所有不同字符
var uniqueWords = from c in array
orderby c
select c.Distinct();
因此,例如,如果您的商品是“Cupcake”,则它的不同之处在于一个 char 数组:
'C','u','p','c','a','k','e'
您需要做的:
(from c in array orderby c select c).Distinct()
我想弄清楚为什么 Distinct() 在这里起作用:
string[] words= new string[] {"Cupcake","Cake","Candy", "Candy"};
var uniqueWords =
from word in words
orderby word
select word;
foreach (var word in uniqueWords.Distinct())
{
Console.Write($" {word}");
但这里没有:
var uniqueWords = from c in array
orderby c
select c.Distinct();
foreach (var element in uniqueWords)
Console.WriteLine($"{element}");
我得到 System.Linq.Enumerable+DistinctIterator`1[System.Char] 控制台输出
我试过转换结果但没有成功。
这是因为你的 select 这里是 select c.Distinct() 就像 select 中字符串 c:
中的所有不同字符var uniqueWords = from c in array
orderby c
select c.Distinct();
因此,例如,如果您的商品是“Cupcake”,则它的不同之处在于一个 char 数组:
'C','u','p','c','a','k','e'
您需要做的:
(from c in array orderby c select c).Distinct()