Reputation: 1120
What I need is to be able to put in a SortedList a combination of two fields that are non-unique, but the pair is (like database composite keys).
More precisly, i have my list, and i want to be able to do this with it
Dim myList = New Generic.SortedList(Of String, String)
myList.add("aaa","aaa")
myList.add("aaa","bbb")
myList.add("bbb","bbb")
myList.add("bbb","aaa")
This would throw an exception as "an entry with the same key already exists"
I know i could make my own object and a list of that object, but is there anything that already exists for what i want to do?
Thanks
Upvotes: 1
Views: 292
Reputation: 8116
You could use the Lookup class. Jon Skeet provided a good answer on that subject. c# duplicate Keys in .NET dictionaries?
You first have to store them in a enumerable object though IEnumerable(Of (Tuple(Of String,String))
for example)
Dim compositeKeys = New List(Of Tuple(Of String, String))
compositeKeys.Add(New Tuple(Of String, String)("aaa", "aaa"))
compositeKeys.Add(New Tuple(Of String, String)("aaa", "bbb"))
compositeKeys.Add(New Tuple(Of String, String)("bbb", "aaa"))
compositeKeys.Add(New Tuple(Of String, String)("bbb", "bbb"))
Dim lookup = compositeKeys.ToLookup(Function(x) x.Item1)
Dim aaaKeys = lookup("aaa") // 2 hits
For a specific lookup, there is always LINQ
Dim specificLookup = lookup("aaa").Where(Function(x) x.Item2 = "bbb").FirstOrDefault
Upvotes: 3