Create a dictionary from a named list item

phudgens

Member
Joined
Nov 19, 2013
Messages
19
Programming Experience
10+
I'm thinking there must be a more straightforward way to create a Dictionary (or even better a SortedDictionary) from a named item in an existing list. I am using the following, and it is working - I am just hoping to find a better way to go about it. Can anyone offer some insight?

Thanks,
Paul Hudgens

Dictionary<string, int> WellNames = newDictionary<string, int>();
for (i=0; i<HFData.Count; i++)
{
ThisWell = HFData.WellName;
if (!WellNames.ContainsKey(ThisWell)) WellNames.Add(ThisWell, 0);
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,143
Location
Sydney, Australia
Programming Experience
10+
It looks like you want to create a Dictionary where the keys are the distinct WellName values from your HFData collection and the values are all zero. If that's the case then it might look like this:
var wellNames = HFData.Select(i => i.WellName).Distinct().ToDictionary(s => s, s => 0);
 
Top Bottom