0

I have this code:

var all_books = db.Books;
var result = all_books.Select(a => new { a.bookid, a.booktitle, a.bookisbn}).Take(4).ToList();

var jsonData = new {
                sEcho = param.sEcho,
                iTotalRecords = all_assets.Count(),
                iTotalDisplayRecords = all_assets.Count(),
                aaData = result
            };
return Json(jsonData,JsonRequestBehavior.AllowGet);

And the json returned is in the form of:

{"sEcho":null,"iTotalRecords":2764,"iTotalDisplayRecords":2764,"aaData":[{"bookid":"M-711745","booktitle":"20844","bookisbn":"HP LA2205wg"},{"bookid":null,"booktitle":"n/a","bookisbn":"n/a"},{"bookid":"M-710587","booktitle":"20707","bookisbn":"HP LA2205wg"},{"bookid":"735129","booktitle":"21272","bookisbn":"HP LA2205wg"}]}

I would like to return a multidimensional array instead.

{"sEcho":null,"iTotalRecords":2764,"iTotalDisplayRecords":2764,"aaData":[["bookid":"M-711745","booktitle":"20844","bookisbn":"HP LA2205wg"],["bookid":null,"booktitle":"n/a","bookisbn":"n/a"],["bookid":"M-710587","booktitle":"20707","bookisbn":"HP LA2205wg"],["bookid":"735129","booktitle":"21272","bookisbn":"HP LA2205wg"]]}

I have tried this code:

var result = all_books.Select(a => new List<string[]>() { new string[]{ a.bookid, a.booktitle, a.bookisbn }}).Take(4);

But I get this error:

The array type 'System.String[]' cannot be initialized in a query result. Consider using 'System.Collections.Generic.List`1[System.String]' instead.

What am I missing?

1 Answer 1

1

Try this:

var result = all_books
    .Select(a => new List<string> { a.bookid, a.booktitle, a.bookisbn })
    .Take(4)
    .ToList();

When you serialize it, you should get the following JSON, which I think is what you are shooting for:

{
    "sEcho":null,
    "iTotalRecords":2764,
    "iTotalDisplayRecords":2764,
    "aaData":
    [
        ["M-711745","20844","HP LA2205wg"],
        [null,"n/a","n/a"],
        ["M-710587","HP LA2205wg","20707"],
        ["735129","HP LA2205wg","21272"]
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.