0
\$\begingroup\$

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; }
}
\$\endgroup\$
2
  • 5
    \$\begingroup\$ Dictionaries are fast (O(1)) but not handy if you need to find items by more than one 'key'. If the lists will not be large you'd be better of just using a list of objects (classes) using Linq to find items. I also think that is what that someone meant by 'objects' \$\endgroup\$
    – Silvermind
    Apr 16, 2013 at 17:30
  • \$\begingroup\$ That makes sense. I have been thinking about it and I really do not need the objectID and only need either the ert or meter number to reference. The function of the dictionary is to be able to add every meter numbers value at a desired index. I think I will be able to create a dictionary that can use the Meter Number as the key then the value is another dictionary that will have keys from 0-however long the list is and the value as the meter value. \$\endgroup\$
    – user24194
    Apr 16, 2013 at 18:44

1 Answer 1

3
\$\begingroup\$

If you have a collection of Meters 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 Meters, 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");
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.