0

These are fixed data structures that I have to work with, I cannot redefine or change them sadly.

I'm looping over a list with each of the objects in the list looking as follows (hover over in Visual Studio).

{[ida: 828, idb: 133, XXX.XXX.XXX.MyObject]}
    Key: {ida: 828, idb: 133}
    Value: {XXX.XXX.XXX.MyObject}

How would I look up value in this list by key, i.e. say how to a lookup a value given one of the keys such as ida=828. I can get the key-values out as I go through the list, but is there anyway to do this faster? I.e. given 828 I can obtain Value, rather like ida=828 or object.key.ida[828]

One of the issues is these datatypes are not changeable. Any suggestions. Is there only way to get at say ida to loop around all the objects?

I know I can use .Equals to determine whether a key value exists, i.e. object.key.ida.Equals(828), but this still leaves the question of getting at the Value. Is this possible?

The data structure looks like the following:

Collection<string, Key, Tstuff>

public struct Key
    {
        public long ida;
        public long idb;
    }

Tstuff is a generic type, but in this case is being passed in the following (effectively the value as above):

public struct D2
{
    public float A;
    public float B;

}
disruptive
  • 5,687
  • 15
  • 71
  • 135
  • possible duplicate of [Multi-key dictionary in c#?](http://stackoverflow.com/questions/1171812/multi-key-dictionary-in-c) – Ryan Gates Mar 27 '14 at 15:53

2 Answers2

0

I'm a little confused as to what you're trying to accomplish (you don't mention idb anywhere in your question), but if the unique combination of ida and idb is what should get you to the value, then you could use a Tuple<int, int> as the key:

var lookup = new Dictionary<Tuple<int, int>, MyObject>();
lookup[Tuple.Create(828, 133)] = MyObjectInstance;

However, if your intention is to get at the value given either key (ida or idb) then you'd be best off just creating two dictionaries:

var lookupByA = new Dictionary<int, MyObject>();
var lookupByB = new Dictionary<int, MyObject>();
lookupByA[828] = MyObjectInstance;
lookupByB[133] = MyObjectInstance;

Then you could just check both dictionaries.

itsme86
  • 19,266
  • 4
  • 41
  • 57
  • I'm not interested in idb at the moment, that comes along for the ride. These data structures already exist, I'm trying to work with them. I cannot make changes to them. – disruptive Mar 27 '14 at 16:43
  • What is the data structure? Is that a class? Let's see the actual code that defines the data structure. And how is the list of objects defined? Is it actually a `List`? – itsme86 Mar 27 '14 at 16:46
  • @itsmen86 I've added more above. Hopefully that should help a little more? – disruptive Mar 28 '14 at 15:01
0

It looks to me like the data structure which you are retrieving your KeyValuePairs from is a dictionary, with a key which is an object made up of two parts - an ida and an idb. If this is the case, you can probably get constant time lookup if you know both parts of the key (ida and idb). Something like:

var myObject = theDictionary[new WhateverKeyIsCalled(828, 133)]

This assumes that you can actually "new" up the keys in the dictionary, that you know the value of both ida and idb, and that the key has a constructor which takes an ida and an idb. All big assumptions on my part, but I'd need to know more about your problem to give a better answer.

Otherwise you'd have to iterate over each keyvaluepair in your dictionary. Like:

foreach(var keyValuePair in theDictionary)
{
     if(keyValuePair.key.ida == 828)
          return keyValuePair.Value;
}

This will be O(n) with the number of items in the dictionary (which is a bit silly for a dictionary), and also, you might have multiple values in your dictionary which have an ida of 828, so that could throw a wrench in things.

tandersen
  • 365
  • 2
  • 10