Last post Jul 10, 2013 12:15 PM by 1jus
Jul 07, 2013 12:17 PM|1jus|LINK
Hi, Im trying to output a number of results obtained from a foreach loop iterating over some SQL results. The problem is when doing so I'm missing the trailing comma needed to make the JSON output valid. Therefore it does not render when received by JQuery.
Heres the code I have, any ideas how to append a comma to all but the last results would be welcomed :)
IEnumerable<dynamic> testData = db.Query("exec dbo.Listing @0", catId);
foreach (var item in testData)
sql = "exec dbo.Listing_View @0";
IEnumerable<dynamic> adContent = db.Query(sql, item.AdvertID);
var returnData = adContent.Select(x => new
fileName = x.PrimaryImage,
county = x.County,
advertId = x.AdvertID,
startDate = x.DateAdded.ToString("dd MMMM yyyy"),
Response.ContentType = "application/json";
Jul 08, 2013 04:01 AM|sam_xiii|LINK
you can add a comma to separate all you items by using the string.Join method (http://msdn.microsoft.com/en-us/library/57a79xd0.aspx)
http://www.sambeauvois.be | @sambeauvois
Jul 08, 2013 04:55 AM|1jus|LINK
I gave that a go but get an ambiguous error notice :(
Error 9 The call is ambiguous between the following methods or properties: 'string.Join(string, params object)' and 'string.Join<AnonymousType#1>(string, System.Collections.Generic.IEnumerable<AnonymousType#1>)'
Jul 08, 2013 05:03 AM|1jus|LINK
Do you think there could be a way to iterate over the data and do something like:
returnData += returnData
for each iteration?
Then take that string and output it as JSON?
Jul 10, 2013 09:02 AM|sam_xiii|LINK
you can use a List that you declare outside of your loop,
use a ToList instead of a ToArray and append the result to the "global" List using the AddRange method
After that all your items are in a single list.
Maybe you can also write an other query that returns you directy what you want.
Jul 10, 2013 12:15 PM|1jus|LINK
I will give that a try this week :)