Brian Muscat
Brian Muscat

Reputation: 5

Append items to an object

I have created a Linq statement to get a list of items from the database. So I need to loop trough the query and append to object to then serialize to then be able to use as json in javascript. The problem is I cannot append to the declared object 'obj'. Can anyone help??

DataContext dataContext = new DataContext();
        var query = from qr in dataContext.tblStocks
                    where qr.enable == true
                    select qr;

        var obj = new JObject();

        foreach (var item in query)
        {
            //obj = new JObject();
            obj = ( new JObject(
                    new JProperty("stockID",item.stockID),
                    new JProperty("itemDepartmentID", item.itemDepartmentID),
                    new JProperty("item" , item.item),
                    new JProperty("description", item.description),
                    new JProperty("stockAmount", item.stockAmount),
                    new JProperty("priceExlVat", item.priceExlVat),
                    new JProperty("vat", item.vat),
                    new JProperty("priceIncVAT", item.priceIncVAT),
                    new JProperty("upc1", item.upc1),
                    new JProperty("upc2", item.upc2)
                ));


       }
        var serialized = JsonConvert.SerializeObject(obj);
        return serialized;

Upvotes: 0

Views: 154

Answers (3)

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

Alternative you could use an anonymous type in the query and just serialize the entire query for the query returns an IEnumerable<T> and this is converted automatically to an jsonArray:

DataContext dataContext = new DataContext();
var query = dataContext.tblStocks
    .Where(stockItem => stockItem.enable)
    .Select(stockItem => new
                        {
                            stockItem.stockID,
                            stockItem.itemDepartmentID,
                            stockItem.item,
                            stockItem.description,
                            stockItem.stockAmount,
                            stockItem.priceExlVat,
                            stockItem.vat,
                            stockItem.priceIncVat,
                            stockItem.upc1,
                            stockItem.upc2
                        });
return JsonConvert.SerializeObject(query);

Upvotes: 0

stefankmitph
stefankmitph

Reputation: 3306

Why don't you just serialize your object?

List<dynamic> obj = new List<dynamic>();
foreach(var item in query) {
    obj.Add(new
    {
        itemDepartmentID = item.itemDepartmentID,
        description = item.description,
        ...
    });
}

var serialized = JsonConvert.SerializeObject(obj);

Upvotes: 2

Richard
Richard

Reputation: 109180

You are reassigning obj each time through the loop hence all other data will be lost.

Easier to create an array:

obj = new JArray();

foreach (var item in query) {
  obj.Add(new JObject(
            new JProperty(...),
            ...));
}

Upvotes: 5

Related Questions