在VB中使用LINQ删除重复项。净

时间:2022-04-06 07:39:07

I'm using a List(of String) which contains the following:

我正在使用List(of String),其中包含以下内容:

1
2
3
1
4
2
5
3
1

I am trying to remove the duplicates with LINQ. But til now I cant see any chance to manage it :(

我试图用LINQ删除重复项。但直到现在我看不出任何机会来管理它:(

I tried:

我试过了:

StringList = StringList.Distinct()

but this causes an fatal error.

但这会导致致命的错误。

1 个解决方案

#1


4  

I'm guessing the problem is that you're trying to assign the result of Distinct() (which is an IEnumerable(Of String)) to an variable of type List(Of String).

我猜测问题是你试图将Distinct()(它是一个IEnumerable(Of String))的结果分配给List(Of String)类型的变量。

Try this:

尝试这个:

Dim StringList As List(Of String) = ...
StringList = StringList.Distinct().ToList()

#1


4  

I'm guessing the problem is that you're trying to assign the result of Distinct() (which is an IEnumerable(Of String)) to an variable of type List(Of String).

我猜测问题是你试图将Distinct()(它是一个IEnumerable(Of String))的结果分配给List(Of String)类型的变量。

Try this:

尝试这个:

Dim StringList As List(Of String) = ...
StringList = StringList.Distinct().ToList()