2

我正在这样做:

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);

结果是这样的:

[{
    "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
    },
}]

我想要这样的结果:

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

这可以使用 JavaScriptSerializer 轻松完成,还是可以重新编写 LINQ 语句来生成它?

更新:感谢这样的一些帖子......

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

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

我不会使用 JavaScriptSerializer,ASP.Net 会为我完成这一切:

[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;
}
4

2 回答 2

2

你可以这样做,但你需要使用一个组来填充项目。

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);

那是从臀部拍摄的,所以如果它不适合你,请告诉我。

于 2011-05-25T14:44:13.600 回答
1

可能有更好的方法,但 Nix 的回答帮助我想出了这个:

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);
于 2011-05-25T15:31:43.097 回答