我可以让 JavaScriptSerializer 分层序列化 LINQ 结果吗?

发布于 2024-11-09 19:27:14 字数 2392 浏览 0 评论 0原文

我正在这样做:

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

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",    
        "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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟─花易冷 2024-11-16 19:27:14

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

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

这是一个突然的想法,所以如果它不适合你,请告诉我。

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.

别挽留 2024-11-16 19:27:14

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文