1

How do I join a list of strings with guids into a dictionary?

I've got a list like so:

new List<String> { "alex", "liza", "harry" }

I'd like to create a dictionary like:

new Dictionary<Guid, String> {
        {
            "97085fe7-4717-4b70-95aa-80be9b1bfb47",
            "alex"
        },
        {
            "f88a41c2-27ac-47c5-a967-a4cdc2c6171c",
            "liza"
        },
        {
            "2a6adaac-23b4-4d6e-9fb4-610d4c14e231",
            "harry"
        }
    }

How do I zip a random list of Guids together with a list of strings?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • 1
    Possible duplicate of [C# Convert List to Dictionary](https://stackoverflow.com/questions/11581101/c-sharp-convert-liststring-to-dictionarystring-string) – Mick May 23 '19 at 00:59
  • also... begs to question: why would you want this? because the context of your question might be leading to another question and another and completely different of your initial question – riffnl May 23 '19 at 01:12
  • @riffnl indeed, great point hahahha – Alex Gordon May 23 '19 at 14:08

2 Answers2

2

You can use .ToDictionary like so:

var myList = new List<String> { "alex", "liza", "harry" }
var myDictionary = myList.ToDictionary(i => Guid.NewGuid());

>>> Dictionary<Guid, string>(3) { { [06dde7e3-2fdd-4107-a4c5-63bee818abda], "alex" }, { [93029465-11b5-463b-ab04-bebdb1a8e77f], "liza" }, { [8ff8ff05-7bcf-4dd0-bb8d-07231932c3d8], "harry" }
Loocid
  • 6,112
  • 1
  • 24
  • 42
1

Try the following:

var names = new List<String> { "alex", "liza", "harry" };

var d = names.ToDictionary(n => Guid.NewGuid());