-1

I am new to C#. Can anyone help me to solve my problem?

My question is

I have a list of dict in some x variable. My dict look like something like below.

x = [
        {
            "name": "rtu_dnp3",
            "ipv4": "10.46.224.109/32",
            "loopback": "yes",
            "sap": "1/3/1",
            "type": "serial"
        },
        {
            "name": "rtu_ea",
            "ipv4": "10.46.224.109/32",
            "loopback": "yes",
            "sap": "1/3/2",
            "type": "serial"
        }
]

Using this dictionary I want to create another dictionary. This I do easily in python.

y = []
for i in x:
    dict = {}
    dict["name"] = i["name"]
    dict["ipv4"] = i["ipv4"]
    y.append(dict)

So can anyone help me how to do in C# Code? So I can play with this dictionaries to create my own format.

Dhananjaya D N
  • 347
  • 2
  • 11
  • possible duplicate: https://stackoverflow.com/questions/61794865/creating-multiple-dictionary-and-appending-into-list-using-c-sharp/61795007#61795007 – David May 14 '20 at 11:01
  • It seems, that you'll need to deserialize a json into custom dictionary – Pavel Anikhouski May 14 '20 at 11:02
  • 1
    It's not clear what you really have here. A JSON string? Or an array of `Dictionary` instances? A `List` of `Dictionary` instances? [Edit] the question and provide a [example] i.e. real and complete C# code that actually compiles. – sticky bit May 14 '20 at 11:05
  • this is json data. I am looping through my c# code. but I am not able to create dictionary-like I explain above in c#. – Dhananjaya D N May 14 '20 at 11:08
  • Does this answer your question? [creating multiple dictionary and appending into list using c#](https://stackoverflow.com/questions/61794865/creating-multiple-dictionary-and-appending-into-list-using-c-sharp) – B--rian May 14 '20 at 15:11

2 Answers2

1

You can use tuple for your dictionary like,

Dictionary<string, Tuple<string, string, string, string>> yourDict = new Dictionary<string, Tuple<string, string, string, string>>();
yourDict.Add("rtu_dnp3", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/1", "serial"));
yourDict.Add("rtu_ea", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/2", "serial"));

Or you can create your model instead of tuple like,

Dictionary<string, YourModel> yourDict = new Dictionary<string, YourModel>();
yourDict.Add("rtu_dnp3", new YourModel("10.46.224.109/32", "yes", "1/3/1", "serial"));
yourDict.Add("rtu_ea", new YourModel("10.46.224.109/32", "yes", "1/3/2", "serial"));

public class YourModel
{
    public string ipv4 { get; set; }
    public string loopback { get; set; }
    public string sap { get; set; }
    public string type { get; set; }

    public YourModel(string ipv4, string loopback, string sap, string type)
    {
        this.ipv4 = ipv4;
        this.loopback = loopback;
        this.sap = sap;
        this.type = type;
    }
}

In this scenario string is your key and YourModel is your value

- UPDATE

Dictionary keeps one TKey and one TValue. So you should use some struct or class (your class or pre-defined classes like Tuple) to keep multiple parameters. Let's contunie with tuple example,

You can copy your dictionary by loop like this,

Dictionary<string, Tuple<string, string, string, string>> yourDict = new Dictionary<string, Tuple<string, string, string, string>>();
yourDict.Add("rtu_dnp3", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/1", "serial"));
yourDict.Add("rtu_ea", new Tuple<string, string, string, string>("10.46.224.109/32", "yes", "1/3/2", "serial"));

var newDict = new Dictionary<string, Tuple<string, string, string, string>>();

foreach (var item in yourDict)
{
    newDict.Add(item.Key,item.Value);
}
Furkan Öztürk
  • 1,178
  • 11
  • 24
  • Hi @Furkan Öztürk, Can u consider my python code and try to give a simple answer using loop and without using any class. It would be helpfull for me – Dhananjaya D N May 14 '20 at 12:02
  • 1
    I hope this will be helpful. For addition, you should give more detail about your case for us to being helpful @DhananjayaDN – Furkan Öztürk May 14 '20 at 12:26
1

Consider the next C# code sample. I think that it is an equivalent of the Python code provided by you:

var x = new List<Dictionary<string, string>>
{
    new Dictionary<string, string>
    {
        ["name"] = "rtu_dnp3",
        ["ipv4"] = "10.46.224.109/32",
        ["loopback"] = "yes",
        ["sap"] = "1/3/1",
        ["type"] = "serial"
    },
    new Dictionary<string, string>
    {
        ["name"] = "rtu_ea",
        ["ipv4"] = "10.46.224.109/32",
        ["loopback"] = "yes",
        ["sap"] = "1/3/2",
        ["type"] = "serial"
    }
};

var y = new List<Dictionary<string, string>>();

foreach (Dictionary<string, string> i in x)
{
    var dict = new Dictionary<string, string>();
    dict["name"] = i["name"];
    dict["ipv4"] = i["ipv4"];
    y.Add(dict);
}

Here is complete sample that also prints resulting list of dictionaries y to the output window.

Here are links to learn about C#'s collections List and Dictionary.

Iliar Turdushev
  • 4,935
  • 1
  • 10
  • 23