2

I have a C# dictionary in which I have a corresponding NAME against the ID.

Dictionary<string, List<object>> dict = new Dictionary<string, List<object>>
{
    { "ID", new List<object> { "Id1", "Id2" } },
    { "NAME", new List<object> { "True", "False" } }
};

foreach (var id in dict["ID"])
{
    Console.WriteLine(id);
    //how to get corresponding "Name". For "Id1" = "True" and for "Id2" = "False"
}

In above code I loop through ID, but how to get corresponding NAME?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
user584018
  • 10,186
  • 15
  • 74
  • 160

4 Answers4

7

I think a better design would be to create a class with the two properties and then iterate. If you find yourself having to sync different data structures for simple data representations then I'd suggest rethinking the design.

public class MyClass
{
    public string Id { get; set; }
    public bool Name { get; set; }
}

And then hold a List<MyClass> which when you iterate:

foreach (var item in list)
{
    // Now access item.Id, item.Name
}

The use of dictionaries is good when you have some sort of natural key for your data and you want to access access an item by that key. As the items are accessed via a hash function accessing by key is done in O(1) whereas searching in a list is O(n). However in your case you are iterating all items in any case so no need for dictionary and arranging the data in a class is a better design.

A bit about the differences and some references:

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
1

If you do have control over dictionary data it's best to either use Gilad's answer and store everything in List<MyClass> or to use Dictionary<string, bool> :

Dictionary<string, bool> dict = new Dictionary<string, bool>()
{
    { "Id1", true }, { "Id2", false },
};

But if you do not have control over format of this data and get it as a dictionary from somewhere (for example web service) you could utilize .Zip method to convert this dictionary into one list of either anonymous objects/custom class or Tuples, where Item1 is Id and Item2 is value:

// anonymous object
var data = dict["ID"].Zip(dict["NAME"], (x, y) => new
{
   ID = x, 
   NAME = y
}).ToList();

// tuple
// List<Tuple<object, object>> data = dict["ID"].Zip(dict["NAME"], Tuple.Create).ToList();

foreach (var obj in data)
{
   Console.WriteLine(obj.ID + "  " obj.NAME);
}
Fabjan
  • 13,506
  • 4
  • 25
  • 52
1

The other answers are probably what you should do to better structure your code. However, if you need to stick to your original use case, you could do something like this:

//Depending on what you're dealing with: Dictionary<string, List<string>>
Dictionary<string, List<object>> dict = new Dictionary<string, List<object>>{
    {"ID", new List<object>{"Id1", "Id2"}}, 
    {"NAME", new List<object>{"True", "False"}}
};

foreach(var v in dict.Keys){            
    Console.WriteLine($"{v} = {string.Join(",", dict[v])}");
}

//Output:
//ID = Id1,Id2
//NAME = True,False
EdSF
  • 11,753
  • 6
  • 42
  • 83
0

Even if you have the just the mapping of ID and Name you can have very simple variable

  Dictionary<string,string> lookup = new Dictionary<string,string>();
  lookup.Add("ID1","True")

and if Name is Boolean type then replace string to bool in the

  Dictionary<string,bool> lookup = new Dictionary<string,bool>();
Devesh
  • 4,500
  • 1
  • 17
  • 28