C#:“'System.Collections.Generic.Dictionary<object,object>.KeyCollection' 不包含 'ToList' 的定义”错误
C#: "'System.Collections.Generic.Dictionary<object,object>.KeyCollection' does not contain a definition for 'ToList'" error
我有以下代码试图在字典中获取值,但似乎无法正常工作。
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace DemoTests
{
class Program
{
static void Main(string[] args)
{
string jsonString = "{\"Name\":\"Bob Smith\",\"mainTitle\":\"Title1\",\"emailList\":[\"test.e@example.com\"],\"rowCount\":\"4\",\"emailSubject\":\"Test Email\",\"items\":{\"sheets\":[[{\"ID\": \"4564342\", \"start\": \"08:00\"}]]}}";
JObject jsonObj = JObject.Parse(jsonString);
Dictionary<dynamic, dynamic> results = jsonObj.ToObject<Dictionary<dynamic, dynamic>>();
List<dynamic> subList = results["items"]["sheets"].ToObject<List<dynamic>>();
foreach (dynamic tableEntries in subList)
{
var entryDict = tableEntries[0].ToObject<Dictionary<dynamic, dynamic>>();
for (int k = 0; k < entryDict.Keys.Count; k++)
{
String key = entryDict.Keys.ToList()[k];
Console.WriteLine(key);
}
}
}
}
}
我在行 String key = entryDict.Keys.ToList()[k];
上遇到错误
Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in System.Linq.Expressions.dll An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll 'System.Collections.Generic.Dictionary<object,object>.KeyCollection' does not contain a definition for 'ToList'
我怎样才能得到它运行?我是 C# 的新手,所以也愿意接受更好的方法来编写我的代码
错误信息告诉你entryDict.Keys没有函数ToList()
.Keys 是一个密钥集合,它使用枚举器遍历它。您不能像 array/list.
那样访问它
你要的是这个:
foreach (var key in results.Keys)
{
Console.WriteLine(key.ToString());
}
使用构造函数创建列表
List<string> keyList = new List<string>(entryDict.Keys);
for (int k = 0; k < entryDict.Keys.Count; k++){
String key = keysList[k];
Console.WriteLine(key);
}
我有以下代码试图在字典中获取值,但似乎无法正常工作。
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
namespace DemoTests
{
class Program
{
static void Main(string[] args)
{
string jsonString = "{\"Name\":\"Bob Smith\",\"mainTitle\":\"Title1\",\"emailList\":[\"test.e@example.com\"],\"rowCount\":\"4\",\"emailSubject\":\"Test Email\",\"items\":{\"sheets\":[[{\"ID\": \"4564342\", \"start\": \"08:00\"}]]}}";
JObject jsonObj = JObject.Parse(jsonString);
Dictionary<dynamic, dynamic> results = jsonObj.ToObject<Dictionary<dynamic, dynamic>>();
List<dynamic> subList = results["items"]["sheets"].ToObject<List<dynamic>>();
foreach (dynamic tableEntries in subList)
{
var entryDict = tableEntries[0].ToObject<Dictionary<dynamic, dynamic>>();
for (int k = 0; k < entryDict.Keys.Count; k++)
{
String key = entryDict.Keys.ToList()[k];
Console.WriteLine(key);
}
}
}
}
}
我在行 String key = entryDict.Keys.ToList()[k];
Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in System.Linq.Expressions.dll An unhandled exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll 'System.Collections.Generic.Dictionary<object,object>.KeyCollection' does not contain a definition for 'ToList'
我怎样才能得到它运行?我是 C# 的新手,所以也愿意接受更好的方法来编写我的代码
错误信息告诉你entryDict.Keys没有函数ToList() .Keys 是一个密钥集合,它使用枚举器遍历它。您不能像 array/list.
那样访问它你要的是这个:
foreach (var key in results.Keys)
{
Console.WriteLine(key.ToString());
}
使用构造函数创建列表
List<string> keyList = new List<string>(entryDict.Keys);
for (int k = 0; k < entryDict.Keys.Count; k++){
String key = keysList[k];
Console.WriteLine(key);
}