Ankit Agarwal

Ankit Agarwal

  • 1.7k
  • 377
  • 218.3k

How to bind datatable in nested json using LINQ in C#

Oct 22 2021 9:25 AM

Hello,

I want to try datatable to nested json using c# but not getting result according to me.

Output Should be:

[{
    "Semesters": [{
            "Semester": "Semester I",
            "CourseList": [{
                    "Coursename": "Accounting for Managers",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                },
                {
                    "Coursename": "Business Communication",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                }
            ]
        },
        {
            "Semester": "Semester II",
            "CourseList": [{
                    "Coursename": "Accounting for Managers",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                },
                {
                    "Coursename": "Business Communication",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                }
            ]
        },
        {
            "Semester": "Semester III",
            "CourseList": [{
                    "Coursename": "Accounting for Managers",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                },
                {
                    "Coursename": "Business Communication",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                }
            ]
        },
        {
            "Semester": "Semester IV",
            "CourseList": [{
                    "Coursename": "Accounting for Managers",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                },
                {
                    "Coursename": "Business Communication",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                }
            ]
        }
    ],

    "Specializations": [{
        "Specialization Name": "Specialization Name",
        "Secialization Semesters": [{
            "Semester": "Semester I",
            "CourseList": [{
                    "Coursename": "Accounting for Managers",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                },
                {
                    "Coursename": "Business Communication",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                }
            ]
        }, {
            "Semester": "Semester II",
            "CourseList": [{
                    "Coursename": "Accounting for Managers",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                },
                {
                    "Coursename": "Business Communication",
                    "Credit": "4",
                    "CourseCode": "ANK001",
                    "Specialization": ""
                }
            ]
        }]
    }]
}]

I have tried:

var semesterData = dt.AsEnumerable().GroupBy(r => new { Semester = r["Semester"] }).Select(c => new
{
    Semester = c.Key.Semester,
    CourseList = c.Select(s => new { CourseName = s.Field<string>("Coursename"), Credit = s.Field<int>("Credit"), CourseCode = s.Field<string>("NewCourseProgarmcode"), Specialization = s.Field<string>("Specialization") }).Where(x => x.Specialization == "").Distinct().ToList(),
    //Specialization = c.Select(s => new { Specialization = s.Field<string>("Specialization"), Coursename = s.Field<string>("Coursename") }).Where(x => x.Specialization != "").Distinct().ToList()
    }).Distinct().ToList();

    var specializationData = dt.AsEnumerable().GroupBy(r => new { SpecializationName = r["Specialization"]} ).Select(c => new
    {
        SpecializationName = c.Select(s => new { SpecializationName = s.Field<string>("Specialization") }).Where(x => x.SpecializationName != "").Distinct().ToList(),
                    
        //CourseName = c.Select(s => new { Coursename = s.Field<string>("Coursename"), Specialization = s.Field<string>("Specialization") }).Where(x => x.Specialization == "").Distinct().ToList() 
    }).Distinct().ToList();

    var result = semesterData + "," + specializationData;
    return Request.CreateResponse(HttpStatusCode.OK, result);
 

Please help me.

 


Answers (3)