I had a similar problem when I was working on one of my previous projects. Here is what I did:
IList<Product> list = Repository.GetProducts(); var collection = products.Select(product => new { id = product.Id, name = product.Name, detailUrl = product.DetailUrl, imageLargeUrl = product.ThumbNailUrl, tagtitle = product.Name.ToUpper(), tagheader = "Words our cherished patrons use to describe this product", tagwords = from tag in product.Tags group tag by tag.Name into words select new { name = words.Key, weight = words.Count() } }); var result = new {id = inquiry.Id, products = collection, }; return this.Jsonp(result);
Here is what the Json result looks like:
{ "id" : 2, "products" : [{ "id" : "3605970008857", "name" : "TITLE1", "detailUrl" : "http://www.urlhere.com", "tagwords" : [{ "name" : "roses", "weight" : 1 }, { "name" : "cotton", "weight" : 1 }, { "name" : "happy", "weight" : 1 }] }, { "id" : "3605970019891", "name" : "TITLE2", "detailUrl" : "http://www.urlhere.com", "tagwords" : [] }],
}
You can also add any other properties from the referenced objects to the result, as you want, to show it in a Json object :)
Vasile Laur Dec 14 '10 at 6:29 2010-12-14 06:29
source share