0

I'm using MultiValueDictionary from Microsoft.Experimental.Collections by this way:

MultiValueDictionary<string, string> multiValueDictionary = new MultiValueDictionary<string, string>();

multiValueDictionary.Add("Device A", "My Device");
multiValueDictionary.Add("Device A", "/dev/sda");
multiValueDictionary.Add("Device A", "123456789");

How can I retrieve(directly) the elements/values without using any loop?

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
  • Using [`TryGetValue`](https://github.com/dotnet/corefxlab/blob/master/src/Microsoft.Experimental.Collections/Microsoft/Collections/Extensions/MultiValueDictionary.cs#L811) method? – Pavel Anikhouski May 02 '20 at 09:14
  • @PavelAnikhouski it returns collection of `IReadOnlyCollection` with 3 values... then how to retrieve it's elements? – Yousha Aleayoub May 02 '20 at 09:18
  • You can try string.Join(',', multiValueDictionary["Device A"]); – Oguz Ozgul May 02 '20 at 09:20
  • 1
    It depens on what are you going to do with this data. You can also use LINQ and apply some function to all the results. – David May 02 '20 at 09:20
  • @YoushaAleayoub You can use linq, can get enumerator, or create a list of values and access them by index. What is issue with using a loop? Iterating a collection is a very basic operation – Pavel Anikhouski May 02 '20 at 09:21
  • @OguzOzgul `string.Join` while using Collection? that's wrong way / bad idea. – Yousha Aleayoub May 02 '20 at 09:22
  • 2
    Then would you like to update your question and inform us how you are going to use the multi - values? You already retrieved the values of Device A when you access it like dict[] or dict.TryGetValue(), then what? – Oguz Ozgul May 02 '20 at 09:25
  • @OguzOzgul In this case, `TryGetValue(),` returns `IReadOnlyCollection`, but I dont wan't use LINQ to access it's elements. Or I could already use `Dictionary` instead. – Yousha Aleayoub May 02 '20 at 09:32
  • There seems to be no direct way other than hacking into the InnerCollectionView (https://github.com/dotnet/corefxlab/blob/master/src/Microsoft.Experimental.Collections/Microsoft/Collections/Extensions/MultiValueDictionary.cs) and getting the `_collection` field which is a List`1. – Oguz Ozgul May 02 '20 at 09:54

0 Answers0