简体   繁体   English

使用LINQ从Ienumerable创建JSON对象

[英]Creating JSON object from Ienumerable using LINQ

I am trying to use LINQ to JSON to create a JSON object using the http://james.newtonking.com/json (JSON.NET) framework. 我正在尝试使用LINQ to JSON使用http://james.newtonking.com/json(JSON.NET )框架创建JSON对象。

Basically I am trying to create a certain structure from the IEnumerable output. 基本上我试图从IEnumerable输出创建一个特定的结构。 Have something like Products-> Actual,forecast,Target. 有类似产品 - >实际,预测,目标。

The LINQ to XML statement works fine and I can see the result. LINQ to XML语句工作正常,我可以看到结果。 Is it due to deferred execution and lazy evaluation in LINQ to XML? 这是由于LINQ to XML中的延迟执行和延迟评估吗? If yes, how do I solve it. 如果是,我该如何解决。

This is what my LINQ to XML method looks like 这就是我的LINQ to XML方法

IEnumerable resultSet = (xmlDoc.Root.Descendants(ns + "Row").Select(result => new
        {
            MonthYearShortName = (DateTime)result.Element(ns + "Column0"),
            Product = (String)result.Element(ns + "Column1"),
            Actual = (decimal)result.Element(ns + "Column2"),
            Forecast = (decimal)result.Element(ns+"Column3"),
            Target = (decimal)result.Element(ns + "Column4"),
        }));

And this is my LINQ to JSON. 这是我的LINQ to JSON。 The example I am using comes from http://james.newtonking.com/json/help/index.html?topic=html/CreatingLINQtoJSON.htm 我使用的示例来自http://james.newtonking.com/json/help/index.html?topic=html/CreatingLINQtoJSON.htm

JObject rss =
                resultSet.Select(p => new JObject(p.Product,
                    new JProperty("MonthYearShortName", p.MonthYearShortName),
                    new JProperty("Actual", p.Actual),
                    new JProperty("Forecast", p.Forecast),
                    new JProperty("Target", p.Target)));

            Console.WriteLine(rss.ToString());

When I execute the LINQ to JSON statements I get the below error message 当我执行LINQ to JSON语句时,我收到以下错误消息

Error   5   'System.Collections.IEnumerable' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.IEnumerable' could be found 

My usings 我的用法

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Extensions = System.Xml.Linq.Extensions;

Not really sure why I am unable to do a select in the second LINQ to JSON statement. 不确定为什么我无法在第二个LINQ to JSON语句中执行select。

Any help would be great. 任何帮助都会很棒。

Thanks! 谢谢!

The Select extension method is defined on the generic IEnumerable<T> interface (not the non-generic IEnumerable ). Select扩展方法在通用IEnumerable<T>接口(不是非通用IEnumerable )上定义。

For the code to compile, you will need to first call Cast<>() 要编译代码,您需要先调用Cast<>()

JObject rss =
         resultSet.Cast<XElement>().Select(p => new JObject(p.Product,
                    new JProperty("MonthYearShortName", p.MonthYearShortName),
                    new JProperty("Actual", p.Actual),
                    new JProperty("Forecast", p.Forecast),
                    new JProperty("Target", p.Target)));

            Console.WriteLine(rss.ToString());

(or convert to a generic IEnumerable<resultSet> in some other way) (或以其他方式转换为通用的IEnumerable<resultSet>

This code should work if you are trying to get the JSON string: 如果您尝试获取JSON字符串,此代码应该有效:

 var resultSet = (xmlDoc.Root.Descendants("Row").Select(result => new
        {
            MonthYearShortName = (DateTime)result.Element(ns + "Column0"),
            Product = (String)result.Element("Column1"),
            Actual = (decimal)result.Element("Column2"),
            Forecast = (decimal)result.Element("Column3"),
            Target = (decimal)result.Element("Column4"),
        }));

 var json = JsonConvert.SerializeObject(resultSet);
 Console.WriteLine(json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM