I have a class called Order
. One of the field is id
, which is just a unique identifier for an Order
object.
So I do something simple to get those Order Ids
Dim myOrderIds = (From x In Orders
Select x.id)
And I get that the class of myOrderIds
is IEnumerable(Of String), which I expect. Now I want to check if myOrderIds contain the same thing with other OrderIds. So I should put both in a HashSet and use SetEqual right?
Dim myOrderIdSet = myOrderIds.ToHashSet
That's like pretty obviously simple. Don't get simpler than this. I got an array, list, enumerable or whatever of ids, and I want to put it in a hashSet. Very simple
And I got this error
Severity Code Description Project File Line Suppression State
Error BC30521 Overload resolution failed because no accessible 'ToHashSet' is most specific for these arguments:
Extension method 'Public Function ToHashSet() As HashSet(Of String)' defined in 'Enumerable': Not most specific.
Extension method 'Public Function ToHashSet() As HashSet(Of String)' defined in 'MoreEnumerable': Not most specific. nicehash2 C:\Users\teguh\Dropbox\vb.net\gridtrading.vb 22 Active
It seems to me that the method ToHashSet is declared like twice somewhere and somehow they clash. But how can that be? The guy in Microsoft didn't code stuff up properly or what? And what should I do?
CodePudding user response:
It's not Microsoft's fault. The error message says that one of the options is from MoreEnumerable
, which I think is from the MoreLinq
NuGet package, which you must have installed. If you have imported both relevant namespaces then the compiler won't be able to tell which you intended to call. If you can't get rid of that NuGet package or one of the namespace imports in that context then you can't call the method as an extension. If you call it as a regular method then you can qualify the name to disambiguate it, e.g.
Dim myOrderIdSet = System.Linq.Enumerable.ToHashSet(myOrderIds)
The other alterntive would be to use the HashSet(Of T)
constructor that accepts an IEnumerable(Of T)
as an argument.