Home > Net >  Want to create one list with one Id and multiple ProviderId values like
Want to create one list with one Id and multiple ProviderId values like

Time:10-30

I want to create one list with one Id and multiple ProviderId values like

Id ProviderId
1  "val1" 
1  "val2" 

I have a model class as per below :

public class Model
    {
        [JsonProperty]
        public string Id { get; set; }
        [JsonProperty]
        public List<PrefDictionary> ProviderId { get; set; }
    }

Now I have created one list as per below :

List<PrefDictionary> PrefData = data.Select(p => new PrefDictionary()
                                    {
                                        _1 = p.PrefDictionary._1,
                                        _2 = p.PrefDictionary._2
                                    }).ToList();

Here in data I am getting whole json format data and one node is like this below for PrefDictionary.

data = {
Id : 1,
PrefDictionary{
1: "Val1",
2: "Val2"
}}

Now then I have created one more list as per below :

List<Model> PP = data.Select(p => new Model()
                                    {
                                        Id = p.Id,
                                        ProviderId = PrefData.Select(x => new PrefDictionary()
                                        {
                                            _1 = x._1,
                                            _2 = x._2
                                        }).ToList(),
                                    }).ToList();
}

But the problem is I am getting empty in second column list in first I am getting Id but not getting any value in second column.

CodePudding user response:

I'm not 100% I understand JSON format you are expecting. However, if you want a list of providers - each with a list of preferences - then I think you'll need another class to get this to serialize properly:

public class Model
    {
        [JsonProperty]
        public List<Providers> providers { get; set; }
    }


public class Providers
    {
        [JsonProperty]
        public string Id { get; set; }
        [JsonProperty]
        public List<PrefDictionary> ProviderId { get; set; }
    }

This should give you the JSON output:

{
  "Providers": [
    {
      "ID": 1,
      "PrefDictionary": [
        {
          "Val1": "",
          "Val2": ""
        }
      ]
    },
    {
      "ID": 2,
      "PrefDictionary": [
        {
          "Val1": "",
          "Val2": ""
        }
      ]
    }
  ]
}

CodePudding user response:

I suspect the problem could be the way how you deserialize your data object.

Based on your code, I've created the result that contains the PrefDirectonary. enter image description here.

However, one thing I did for the data variable was to make an object like this,

PrefDictionary pd = new PrefDictionary 
{
  _1 = "Value1",
  _2 = "Value2"
};

List<TestObject> data = new List<TestObject>() {
 new TestObject
 {
    Id = "1",
    PrefDictionary = pd
 }
};

It will be helpful to show us how you deserialize the JSON data object.

  • Related