How to format the DbSet object to Json list

How to format the DbSet object to Json list

pradeepkumarhpradeepkumarh Posts: 2Questions: 1Answers: 0

I am converting DbSet object from Entity Framework to json as
var aaData = db.Contacts.ToList();

        return Json(new
        {
            aaData = db.Contacts.Select(x => new { x.ID, x.FirstName, x.LastName })
        }, JsonRequestBehavior.AllowGet);

It is returning me -
{"aaData":[{"ID":2,"FirstName":"first name1","LastName":"last name 1"},{"ID":3,"FirstName":"first name2","LastName":"last name2"}]}

Getting warning message "Requested unknown parameter" message.

What I found is If I send the data as

{"aaData":[["ID":2,"FirstName":"first name1","LastName":"last name 1"],["ID":3,"FirstName":"first name2","LastName":"last name2"]]}

DataTable will load fine.

How to convert Entity object to the required data format. Please suggest me. It would be a great help.

This discussion has been closed.