Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用LINQ从Ienumerable创建JSON对象_C#_Linq_Linq To Xml_Json.net - Fatal编程技术网

C# 使用LINQ从Ienumerable创建JSON对象

C# 使用LINQ从Ienumerable创建JSON对象,c#,linq,linq-to-xml,json.net,C#,Linq,Linq To Xml,Json.net,我正在尝试使用LINQtoJSON来使用(JSON.NET)框架创建JSON对象 基本上,我试图从IEnumerable输出创建一个特定的结构。有一些类似产品->实际、预测、目标 LINQtoXML语句运行良好,我可以看到结果。这是由于LINQtoXML中的延迟执行和延迟计算造成的吗?如果是,我如何解决它 这就是我的LINQ到XML方法的样子 IEnumerable resultSet = (xmlDoc.Root.Descendants(ns + "Row").Select(result =&

我正在尝试使用LINQtoJSON来使用(JSON.NET)框架创建JSON对象

基本上,我试图从IEnumerable输出创建一个特定的结构。有一些类似产品->实际、预测、目标

LINQtoXML语句运行良好,我可以看到结果。这是由于LINQtoXML中的延迟执行和延迟计算造成的吗?如果是,我如何解决它

这就是我的LINQ到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"),
        }));
这是我对JSON的LINQ。我使用的示例来自

当我执行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 
我的使用

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;
不太清楚为什么我无法在第二条LINQ to JSON语句中执行select

任何帮助都会很好


谢谢

选择扩展方法是在通用
IEnumerable
界面上定义的(而不是非通用
IEnumerable

要编译代码,首先需要调用
Cast()

JObject-rss=
resultSet.Cast().Select(p=>newjobject(p.Product,
新的JProperty(“MonthYearShortName”,p.MonthYearShortName),
新产权(“实际”,p.Actual),
新物业(“预测”,p.Forecast),
新物业(“Target”,p.Target));
Console.WriteLine(rss.ToString());

(或者以其他方式转换为通用的
IEnumerable

如果您试图获取
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);

正在检查…您是否有对System.Linq的引用?如果更改JObject rss。。。。改为使用rss?我认为错误可能是因为resultSet.Select(…)选择的是一个对象列表,而不是单个对象。这也不能解决问题:(您正在尝试从反序列化的xml获取JSON字符串吗?是的,我正在尝试创建JSON字符串。现在我面临一个问题,当前xml中不存在名称“Cast”context@Navyseal我编辑了我的答案,你可能需要调整代码,因为我还没有测试过倒霉蛋。属性(p.Product和其他)不可用。我收到一个错误“无法解析符号”@Navyseal它现在编译了吗?老实说,我不知道传递给强制转换的是什么resultSet.cast().select我知道这个。只是我需要先更改字符串的结构。基本上有一些东西,比如Products->Actual,forecast,Target你能在你的问题中发布你希望得到的字符串的格式吗?
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());
 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);