0

I wonder if anyone can help.

I am using the following code to retrieve some data from a database and output it as JSON:

public ActionResult PieDataPropertyTotalsByStatusSO()
{
    var db = new Coyote_AcquisitionsContext();
    IQueryable<Property> query = db.Properties;

    var propertyTotalsByStatus =
        query.GroupBy(p => p.Status).OrderByDescending(p => p.Count()).Select(pg => new { Status = pg.FirstOrDefault().Status.Name, Count = pg.Count() }).ToList();

    return Json(propertyTotalsByStatus, JsonRequestBehavior.AllowGet);
}

This outputs data as follows:

[ 
{ "Count" : 1205,
  "Status" : "General Stock (Rejected)"
},
{ "Count" : 816,
  "Status" : "New Introduction"
},
{ "Count" : 653,
  "Status" : "Potential"
},
{ "Count" : 110,
  "Status" : "Completed"
}
]

However to save bandwidth (and because this is the format the charting library I am using expects) I need to strip out the keys and just end up with:

[ 
[ 1205, "General Stock (Rejected)" ],
[ 816, "New Introduction" ],
[ 653, "Potential" ],
[ 110, "Completed" ]
]

I can't find an elegant way of doing it. I may end up with more or less fields than just Count and Status so I can't use a solution that iterates through on the basis of their being exactly two different fields.

Any help massively appreciated.

EDIT:

Corrected the JSON that I am looking to output.

2
  • The structure at the bottom is not valid json - You could output an array of arrays i.e. [[1205,"General Stock (Rejected)"],...]. Is that a valid option? Commented Feb 4, 2014 at 19:28
  • Yes, my mistake that is what I want. I have edited the question. Commented Feb 4, 2014 at 19:36

2 Answers 2

4

Your desired format is not a valid JSON. You could get following instead:

[ 
    [ 1205, "General Stock (Rejected)" ],
    [ 816, "New Introduction" ],
    [ 653, "Potential" ],
    [ 110, "Completed" ]
]

by

var propertyTotalsByStatus =
    query.GroupBy(p => p.Status)
         .OrderByDescending(p => p.Count())
         .Select(pg => new { Status = pg.FirstOrDefault().Status.Name, Count = pg.Count() })
         .ToList()
         .Select(x => new object[] { x.Status, x.Count })
         .ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

My mistake, your example JSON is exactly what I am after and I have edited my question above. However your code gives me the error: " Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types. " Any ideas?
OK, I've updated my answer. You have to make the second part of query (anonymous type to array) using LINQ to Objects.
That is perfect and solves my issue 100%. Thanks so much for your help. Been tearing my hair out.
0

Sounds like a Dictionary in C#.

var dataDict = propertyTotalsByStatus.ToDictionary(item => item.Status, item => item.Count);
return Json(dataDict , JsonRequestBehavior.AllowGet);

1 Comment

This code didn't work for me but didnt spend much time investigating as MarcinJuraszek answer is exactly what I needed. Thanks for your time anyway.

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.