1

I was assigned with a problematic (at least for me) job for my internship. The project has a C# object that looks like this:

public class Person
{
    int Id { get; set; }
    string Name { get; set; }
    string OtherInfo { get; set; } //json string for a dynamic info
}

So in the database that class' table tblPersons looks like this:

Id       Name            OtherInfo
1       John Doe        {"Gender":"Male","City":"New York"}
2       James Bradley   {"Gender":"Male","City":"Los Angeles"}

So when I say "dynamic info" I meant that the properties of the json string in the otherinfo column may change. This property is defined in another table called 'tblOtherInfoDetails'

Id          Name              Type
1         Gender             Text
2         City               Text

Now to the task, I have to create a Persons list table containing the other info as column in the table. Like so; (DESIRED HTML TABLE OUTPUT)

Id          Name              Gender            City
1         John Doe            Male              New York
2         James Bradley       Male              Los Angeles

But I am puzzled on how to do / display this. I can create another class like:

public class PersonsList
{
    string Name..
    string Gender..
    string City...
}

And populate it with data by deserializing the json string and adding the name to display in the view but the problem is when the OtherInfo details is changed, the program will not work anymore. I need some way to create a dynamic class on the fly that can be populated by different properties and to be displayed in the screen. But I am puzzled on how to do that or if that is possible as I am just new in C#. Any help or suggestion will be highly appreciated..

super-user
  • 1,017
  • 4
  • 17
  • 41

1 Answers1

0

Well, I am not sure, but may be you can create a new Model which contains a list of key value pair. And then you can search through tblOtherInfoDetails for the keys and search for values from the deserialized object matching their keys.And then insert the key-value in your newly made object. Attaching a snippet of code.

Here is your new model class

    public class MyPerson
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public Dictionary<string, string> KeyVal { get; set; }
    }

Then this is what you may try or something similar probably

   public ActionResult Test()
    {
        MyPerson mp= new MyPerson();

        var myTestObj = db.Persons.Find(1);// Say for test you are picking the person obj where the primary key is 1.
        var testOtherInfo = db.tblOtherInfoDetailss.ToList();
        Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(myTestObj.OtherInfo);
        mp.Name = myTestObj.Name;
        mp.KeyVal = new Dictionary<string, string>();
        foreach (var i in testOtherInfo)
        {

            var value = values.Where(a => a.Key == i.Name).First().Value;

            mp.KeyVal.Add(i.Name, value);

        }
        return View(mp);
    }
Bits_Please
  • 546
  • 1
  • 5
  • 13