0

I have 3 datatables in my dataset. Table A has one to many relationship with B and C. I want to create Json like below using Linq in c#. Can someone please help me? I would like to thanks in advance to everyone who will guide me or provide me input to my question.

{
    "A": [
        {
            "id": "0001",
            "type": "donut",
            "name": "Cake",
            "ppu": 0.55,
            "B": [
                {
                    "id": "1001",
                    "type": "Regular"
                }
            ],
            "C": [
                {
                    "id": "5001",
                    "type": "None"
                },
                {
                    "id": "5002",
                    "type": "Glazed"
                }
            ]
        }
    ],
    "A": [
        {
            "id": "0002",
            "type": "Cupcake",
            "name": "Cake",
            "ppu": 2.43,
            "B": [
                {
                    "id": "1001",
                    "type": "Regular"
                }
            ],
            "C": [
                {
                    "id": "5001",
                    "type": "None"
                },
                {
                    "id": "5002",
                    "type": "Glazed"
                }
            ]
        }
    ]
}
Alkingson
  • 13
  • 3
  • Any specific reason you are cutting your 'means' in stone and letting your 'goals' come 2nd? (i.o.w. your goal is the serialized output in JSON format, not "i must use LINQ to do this"). There are many JSON serializers nowadays which can do all the heavy lifting for you. – Marvin Smit Jun 04 '15 at 11:08

2 Answers2

1

First of all, you have to create these classes,

public class Result
{
    public List<TableA> A { get; set; }
}
public class TableA
{
    public TableA()
    {
          B = new List<TableB>();
          C = new List<TableC>();
    }
    public string id { get; set; }
    public string type { get; set; }
    public string name { get; set; }
    public float ppu { get; set; }
    public virtual List<TableB> B { get; set; }
    public virtual List<TableC> C { get; set; }
}

public class TableB
{
    public string id { get; set; }
    public string type { get; set; }
}
public class TableC
{
    public string id { get; set; }
    public string type { get; set; }
}

After then, store the values in result object of Result class and serialize this using NewtonSoft serializer as mentioned below:

Result result;
JsonConvert.SerializeObject(result); 
M.S.
  • 4,283
  • 1
  • 19
  • 42
  • You beat me in that. :) Just don't forget to make your child collections `virtual` so that EF can lazy load them. – vortex Jun 04 '15 at 11:13
0

You can make it simple by without creating any classes and utilizing "dynamic" type. C# 4 allows you to create your types dynamically and use them like any declared types or classes.

For more on dynamic type: http://www.codeproject.com/Articles/69407/The-Dynamic-Keyword-in-C

First thing you need is to create dynamic type and fill its properties like the Json you require. Please see this post to see how the structure of Json and dynamic correlates: http://www.codeproject.com/Tips/834005/storing-GeoJson-coordinates.

There are many ways to make your iteration on DataSet to be dynamically adjust based on your data and columns. You may take further help from: Convert datatable to JSON in C#.

I hope this might help.

Community
  • 1
  • 1
Nabeel
  • 73
  • 9