If you want an immutable collection (You aren't going to be adding to the collection after the collection is created), then use a Lookup. If you need to be able to add/remove entries, then use a dictionary. I've included an example of using IGrouping, but it is the slowest for lookups since you need to iterate through until you find the key, but very efficient if you need to iterate all the keys like you want to pull a random string for every key.
Here's an example of using a Lookup:
var values = new[] {
new KeyValuePair<string,string> ("AAAA","ACFE"),
new KeyValuePair<string,string> ("AAAA","AAFE"),
new KeyValuePair<string,string> ("AAAA","CAED"),
new KeyValuePair<string,string> ("AAAA","HSGB"),
new KeyValuePair<string,string> ("AAGB","ZZZZ"),
new KeyValuePair<string,string> ("AAGB","XXXX"),
new KeyValuePair<string,string> ("AAGB","CAED"),
new KeyValuePair<string,string> ("AAGB","HSGB"),
new KeyValuePair<string,string> ("ABDA","YYYY"),
new KeyValuePair<string,string> ("ABDA","WWWW"),
new KeyValuePair<string,string> ("ABDA","CAED"),
new KeyValuePair<string,string> ("ABDA","HSGB"),
};
// Convert array to a Lookup
var lookup=values.ToLookup(k=>k.Key, v=>v.Value);
// Retrieve random string from entry "AAAA"
var entry = lookup["AAAA"];
var rand = new Random();
var max = entry.Count();
var ans = entry.Skip(rand.Next(max)).First();
Here's an example of using a Dictionary<string,List<string>>
:
var values = new[] {
new KeyValuePair<string,string> ("AAAA","ACFE"),
new KeyValuePair<string,string> ("AAAA","AAFE"),
new KeyValuePair<string,string> ("AAAA","CAED"),
new KeyValuePair<string,string> ("AAAA","HSGB"),
new KeyValuePair<string,string> ("AAGB","ZZZZ"),
new KeyValuePair<string,string> ("AAGB","XXXX"),
new KeyValuePair<string,string> ("AAGB","CAED"),
new KeyValuePair<string,string> ("AAGB","HSGB"),
new KeyValuePair<string,string> ("ABDA","YYYY"),
new KeyValuePair<string,string> ("ABDA","WWWW"),
new KeyValuePair<string,string> ("ABDA","CAED"),
new KeyValuePair<string,string> ("ABDA","HSGB"),
};
//Convert array to Dictionary<string,List<String>>
var dict = values.GroupBy(k=>k.Key)
.ToDictionary(k=>k.Key,v=>v.Select(kvp=>kvp.Value).ToList());
// Retrieve random string from entry "AAAA"
var entry = lookup["AAAA"];
var rand = new Random();
var max = entry.Count();
var ans = entry.Skip(rand.Next(max)).First();
Here's an example using IGrouping:
var values = new[] {
new KeyValuePair<string,string> ("AAAA","ACFE"),
new KeyValuePair<string,string> ("AAAA","AAFE"),
new KeyValuePair<string,string> ("AAAA","CAED"),
new KeyValuePair<string,string> ("AAAA","HSGB"),
new KeyValuePair<string,string> ("AAGB","ZZZZ"),
new KeyValuePair<string,string> ("AAGB","XXXX"),
new KeyValuePair<string,string> ("AAGB","CAED"),
new KeyValuePair<string,string> ("AAGB","HSGB"),
new KeyValuePair<string,string> ("ABDA","YYYY"),
new KeyValuePair<string,string> ("ABDA","WWWW"),
new KeyValuePair<string,string> ("ABDA","CAED"),
new KeyValuePair<string,string> ("ABDA","HSGB"),
};
//Convert array to IGrouping<string,string>
var group = values.GroupBy(k=>k.Key);
// Retrieve random string from entry "AAAA"
var entry = group.First(k=>k.Key == "AAAA");
var rand = new Random();
var max = entry.Count();
var ans = entry.Skip(rand.Next(max)).First().Value;