C# 使用 LINQ 从数组中选择不同的单词列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2259641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Distinct List of Words from Array with LINQ
提问by DaveDev
I'm trying to get a distinct list of words from an array of words with the following code:
我正在尝试使用以下代码从一组单词中获取一个不同的单词列表:
string words = "this is a this b";
var split = words.Split(' ');
IEnumerable<Word> distinctWords = (from w in split
select new Word
{
Text = w.ToString()
}
).Distinct().ToList();
I thought this would take out the double occurrence of 'this' but it returns a list of each word int he phrase.
我认为这会消除“this”的双重出现,但它会返回短语中每个单词的列表。
Can somebody please suggest how I can go about getting a distinct list? Thanks
有人可以建议我如何获得一个不同的列表吗?谢谢
Dave
戴夫
采纳答案by James Curran
In your example, each Word object isdistinct, because there is no comparison which looks at the Text property.
在您的示例中,每个 Word 对象都是不同的,因为没有查看 Text 属性的比较。
However, there's no reason to create a new object:
但是,没有理由创建一个新对象:
var distinctWords = (from w in split
select w).Distinct().ToList();
Or more simply:
或者更简单:
var distinctWords = new List<string>(split.Distinct());
回答by Mark Byers
You haven't posted the code for your Word
class, but my guess is that it doesn't implement Equals
with a value comparison so you get the default implementation of Equals
which just checks the object references. Note that if you decide to implement your own version of Equals
, you also need to correctly implement GetHashCode
.
你还没有发布你的Word
类的代码,但我的猜测是它没有Equals
通过值比较来实现,所以你得到的默认实现Equals
只是检查对象引用。请注意,如果您决定实现自己的版本Equals
,则还需要正确实现GetHashCode
.
An alternative way to solve this issue is to provide an IEqualityComparer
as a parameter to the Distinct
function.
解决此问题的另一种方法是提供一个IEqualityComparer
作为Distinct
函数的参数。
回答by citronas
The problem is, that you create several Word objects that contain the same Value, but how should the compiler know, that these shall be the same items?
问题是,您创建了多个包含相同 Value 的 Word 对象,但是编译器如何知道这些是相同的项目?
Try
尝试
(from w in split.Distinct()
select new Word { Text = w.ToString()}).ToList();
回答by Tomas Petricek
As others noted, the problem is probably that your Word
object doesn't implement structural equality (compare the actual content, not instance references). If you still want to get a collection of Word
objects as the result, but use Distinct
on the underlying string values, you can write this:
正如其他人指出的那样,问题可能是您的Word
对象没有实现结构相等(比较实际内容,而不是实例引用)。如果你仍然想得到一个Word
对象集合作为结果,但Distinct
在底层字符串值上使用,你可以这样写:
IEnumerable<Word> distinctWords =
(from w in split.Distinct()
select new Word { Text = w.ToString() }).ToList();
回答by M.Laida
You may try to convert array ToList() first before calling the .Distinct() and then converting it ToArray() again
您可以尝试在调用 .Distinct() 之前先将数组 ToList() 转换,然后再次将其转换为 ToArray()
myArray= myArray.ToList().Distinct().ToArray();