开发者

Can I get JavaScriptSerializer to serialize a LINQ result hierarchically?

开发者 https://www.devze.com 2023-03-08 03:10 出处:网络
I\'m doing this: var data = from a in attributes from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )

I'm doing this:

var data = from a in attributes
           from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
           .DefaultIfEmpty(new DocClassAttributeFieldItem())
                select new
                {
                    Id = a.Id,
                    LabelText = a.LabelText,
                    Items = i
                };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);

The result is this:

[{
    "Id": 1,
    "LabelText": "Unit On-Line Status:",
    "Items": {
        "Id": 1,
        "DocClassAttributeFieldId": 1,
        "LabelText": "Online",
        "ValueText": "Online",
        "Ordering": 1
    }
},
{
    "Id": 1,
    "LabelText": "Unit On-Line Status:",
    "Items": {
        "Id": 2,
        "DocClassAttributeFieldId": 1,
        "LabelText": "Offline",
        "ValueText": "Offline",
        "Ordering": 2
    },
}]

I'd like to have a result like this:

[{    
    "Id": 1,    
    "LabelText": "Unit On-Line Status:",    
    "Items": [{    
        "Id": 1,    
        "DocClassAttributeFieldId": 1,    
        "LabelText": "Online",    
        "ValueText": "Online",   开发者_JS百科 
        "Ordering": 1    
    },{    
        "Id": 2,    
        "DocClassAttributeFieldId": 1,    
        "LabelText": "Offline",    
        "ValueText": "Offline",    
        "Ordering": 2    
    }]    
}]

Can this be done easily with JavaScriptSerializer or can the LINQ statement be reworked to produce this?

Update: Thanks to some posts like these...

http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

I'm not going to use the JavaScriptSerializer, ASP.Net does it all for me:

[WebMethod]
public static object GetDocClass(int docClassId)
{
    var data = from a in attributes                       
        select new
        {
            Id = a.Id,
            LabelText = a.LabelText,
            Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)                    
        };          
    return data;
}


You can do it but you need to use a group into to populate items.

var data = 
       from a in attributes
       from i in attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id )
       group a by a.Id, a.LabelText into myGroup
       .DefaultIfEmpty(new DocClassAttributeFieldItem())
            select new
            {
                Id = a.Id,
                LabelText = a.LabelText,
                Items = myGroup.ToList()
            };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);

That was a shot from the hip, so let me know if it doesn't work for you.


There is probably a better way, but Nix's answer helped me come up with this:

var data = from a in attributes                       
            select new
            {
                Id = a.Id,
                LabelText = a.LabelText,
                Items = attributeItems.Where(x=>x.DocClassAttributeFieldId == a.Id)                    
            };

JavaScriptSerializer serializer = new JavaScriptSerializer();
TextBox1.Text = serializer.Serialize(data);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号