views:

51

answers:

1

Python's equivalent of what I want is:

>>> #C#: Dictionary<int, string> tempDict = ...
>>> tempDict = {i : str(i) for i in range(200000)}
>>> tempDict[5]
'5'
>>> 

The example is a bit simplified, but I can modify it myself; do not want to bother you with details of proprietary classes.

Got it:

var y = (from x in Enumerable.Range(0, 20000) select Guid.NewGuid()).ToDictionary(g=>g, g=>new MyObj(g))
+6  A: 
Enumerable.Range(0, 200000).ToDictionary(x => x, x => x.ToString())

maybe?

Joey
Thank you. Now ... what if I wanted to not use an index at all? The key must be a Guid guid = Guid.NewGuid(), and a value is ... say guid.ToString() or myFunc(guid) - the point is that A) Guids are given to me by the system, B) the value depends on the key (guid). Would it make sense to get a generator of guids and then feed it into a dict? I know that one-liners can get ugly, but I am learning LINQ and am curious if it is possible. Thanks.
Hamish Grubijan
I guess I want something like Enumerable.Repeat<Guid>( () => Guid.NewGuid(), 200000) except that Repeat does not take a lambda but a value. Also, I do not want to use an intermediate ToList - I want to keep stuff as IEnumerable untill I need to generate the actual dictionary.
Hamish Grubijan
Hm, sounds like you're better off with writing an enumerator that generates 200k GUIDs. You can easily do something like `myfunc(guid)` in the `ToDictionary` call.
Joey