Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please help with a dictionary comprehension in LINQ, C#

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))
like image 334
Hamish Grubijan Avatar asked Jan 26 '26 20:01

Hamish Grubijan


1 Answers

Enumerable.Range(0, 200000).ToDictionary(x => x, x => x.ToString())

maybe?

like image 111
Joey Avatar answered Jan 29 '26 12:01

Joey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!