I am brand new to dictionaries and coding and am hoping to get some suggestions on how I should structure my dictionary.
I have a collector number which has meters connected to them. Each of the meters have a meter number and a ert number and would like to store both the meter and ert number. There will be a list of meter reading timestamps and values for each of the meters. Also, I would like to be able to select the values by either the ert number or meter number. I have attached the current structure I have for the dictionary but was wondering if this was the best way to format the dictionary. Someone had suggested objects but I have never worked with objects before.
{
Dictionary<Int32, Dictionary<string, Meter>> dictionary = new Dictionary<int, Dictionary<string, Meter>>();
if (!dictionary.ContainsKey(objectID))
{
dictionary.Add(objectID, new Dictionary<string, Meter>());
}
Meter meter = new Meter();
meter.ertNumber = "9480:1";
meter.meterNumber = "fsadfa";
meter.Reading = new Dictionary<Int32, double>();
meter.Reading.Add(0, 6);
dictionary[objectID].Add("Test", meter);
}
class Meter
{
public String ertNumber { get; set; }
public String meterNumber { get; set; }
public Dictionary<Int32, double> Reading { get; set; }
}
1 Answer 1
If you have a collection of Meter
s and you want to be able to find the ONE based on a property, this is how I would do it:
Create a List
of Meter
s, say l
..
List<Meter> l = new List<Meter>();
Then implement Extension methods like so..
public static class MeterExt
{
public static Meter GetByErm(this List<Meter> l, String sErm)
{
Meter r = l.Find(s => s.ertNumber == sErm);
return r;
}
public static Meter Get(this List<Meter> l, String sMeter)
{
Meter r = l.Find(s => s.meterNumber == sMeter);
return r;
}
}
Then you can find your meter like so..
Meter m = l.Get("fsadfa");
Linq
to find items. I also think that is what that someone meant by 'objects' \$\endgroup\$