Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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中列出<;字典<;对象、对象>;,以提取数据。)_C#_Linq - Fatal编程技术网

C# (在Linq中列出<;字典<;对象、对象>;,以提取数据。)

C# (在Linq中列出<;字典<;对象、对象>;,以提取数据。),c#,linq,C#,Linq,我有一个数据定义 我将JSON反序列化到此对象 @返回是JSON JsonConvert.DeserializeObject<List<Dictionary<Object, Object>>>(utils.RemoveJsonOuterClass("GetTable", JsonConvert.DeserializeObject(@return).ToString())); olist = [ [{ "item": 1

我有一个数据定义

我将JSON反序列化到此对象

@返回是JSON

JsonConvert.DeserializeObject<List<Dictionary<Object, Object>>>(utils.RemoveJsonOuterClass("GetTable", JsonConvert.DeserializeObject(@return).ToString()));

olist = [
    [{
        "item": 1
        "Name "One"
    }],
    [{
        "item": 2
        "Name "Two"
    }],
    [{
        "item": 1
        "Name "One Two"
    }]
];
JsonConvert.DeserializeObject(utils.RemoveJsonOuterClass(“GetTable”,JsonConvert.DeserializeObject(@return.ToString()));
寡头=[
[{
“项目”:1
“一个名字”
}],
[{
“项目”:2
“两个名字”
}],
[{
“项目”:1
“名字”一二“
}]
];
这是一个
列表

我需要找到
“item”==1
的所有项目


我可以使用Linq吗?或者在使用大量数据时还有其他方法吗?

首先:您的
json
不正确,请修复此问题

  • 名称和值之间应该有一个
    冒号
  • 项目值后应出现
    逗号
然后按如下所示更改代码

//Create a class matching response object
public class ResponseItem
{
    [JsonProperty("item")]
    public int Item { get; set; }
    public string Name { get; set; }
}

var responseJson = utils.RemoveJsonOuterClass("GetTable", 
JsonConvert.DeserializeObject(@return).ToString();

var responseData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<ResponseItem, ResponseItem>>>(responseJson);
其中
是延迟执行,在每个循环上,它返回一个对象。 这是我在当地执行的截图


不知道您对对象类型是否正确。但这项任务很容易解决:

static void Main(string[] args)
{
    // Build the object
    List<Dictionary<int, TestObject>> list = new List<Dictionary<int, TestObject>>();

    // fill it with dictionaries
    list.Add(new List<TestObject>()
    {
        new TestObject(){ Id = 1, Name = "One" },
        new TestObject() { Id = 2, Name = "Two" },
        new TestObject() { Id = 3, Name = "Three" }
    }.ToDictionary(d => d.Id));

    list.Add(new List<TestObject>()
    {
        new TestObject() { Id = 2, Name = "Two" },
        new TestObject() { Id = 3, Name = "Three" }
    }.ToDictionary(d => d.Id));

    list.Add(new List<TestObject>()
    {
        new TestObject(){ Id = 1, Name = "One" },
        new TestObject() { Id = 2, Name = "Two" }
    }.ToDictionary(d => d.Id));

    // Let's build a single list to work with
    IEnumerable<TestObject> completeList = list.SelectMany(s => s.Values);

    // aaaand filter it
    IEnumerable<TestObject> filteredList = completeList.Where(l => l.Id == 1);
}

public class TestObject
{
    public int Id { get; set; }
    public string Name { get; set; }
}
static void Main(字符串[]args)
{
//构建对象
列表=新列表();
//用字典填满它
添加(新列表()
{
新建TestObject(){Id=1,Name=“One”},
新建TestObject(){Id=2,Name=“Two”},
新建TestObject(){Id=3,Name=“Three”}
}.ToDictionary(d=>d.Id));
添加(新列表()
{
新建TestObject(){Id=2,Name=“Two”},
新建TestObject(){Id=3,Name=“Three”}
}.ToDictionary(d=>d.Id));
添加(新列表()
{
新建TestObject(){Id=1,Name=“One”},
新建TestObject(){Id=2,Name=“Two”}
}.ToDictionary(d=>d.Id));
//让我们建立一个单独的列表来使用
IEnumerable completeList=list.SelectMany(s=>s.Values);
//A然后过滤它
IEnumerable filteredList=completeList.Where(l=>l.Id==1);
}
公共类测试对象
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}

大部分是初始化;-)

您能提供填充列表的代码吗?因为我没有看到{object,object}的字典,所以我看到了数组?
static void Main(string[] args)
{
    // Build the object
    List<Dictionary<int, TestObject>> list = new List<Dictionary<int, TestObject>>();

    // fill it with dictionaries
    list.Add(new List<TestObject>()
    {
        new TestObject(){ Id = 1, Name = "One" },
        new TestObject() { Id = 2, Name = "Two" },
        new TestObject() { Id = 3, Name = "Three" }
    }.ToDictionary(d => d.Id));

    list.Add(new List<TestObject>()
    {
        new TestObject() { Id = 2, Name = "Two" },
        new TestObject() { Id = 3, Name = "Three" }
    }.ToDictionary(d => d.Id));

    list.Add(new List<TestObject>()
    {
        new TestObject(){ Id = 1, Name = "One" },
        new TestObject() { Id = 2, Name = "Two" }
    }.ToDictionary(d => d.Id));

    // Let's build a single list to work with
    IEnumerable<TestObject> completeList = list.SelectMany(s => s.Values);

    // aaaand filter it
    IEnumerable<TestObject> filteredList = completeList.Where(l => l.Id == 1);
}

public class TestObject
{
    public int Id { get; set; }
    public string Name { get; set; }
}