gpt4 book ai didi

c# - 如何使用 LINQ 展平类中的嵌套字典

转载 作者:行者123 更新时间:2023-11-30 14:52:47 25 4
gpt4 key购买 nike

最接近我所寻找的解决方案是这个线程 How to flatten nested objects with linq expression

但是我在尝试这种方法时遇到错误

The type arguments for method 'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable, System.Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

我的代码:

var aa = t.data.SelectMany(x => 
x.Value.innerData.SelectMany(y => new { /*Error at this SelectMany*/
url = x.Key,
disp = x.Value.disp,
date = y.Key,
count = y.Value.count,
rank = y.Value.rank,
}));

我的类(class):

public class TData {
public Dictionary<string, TDetail> data { get; set; }
}

public class TDetail {
public string disp { get; set; }

[Newtonsoft.Json.JsonProperty("data")]
public Dictionary<string, Metrics> innerData { get; set; }

}

public class Metrics {
public string count { get; set; }
public string rank { get; set; }
}

我从第 3 方 API 获得的 JSON 如下所示:

{
"data": {
"abc.com": {
"disp": "#712176",
"data": {
"2015-02-08": {
"count": 4,
"rank": 5.8
},
"2015-02-23": {
"count": 3,
"rank": 8.3
},
"2015-03-14": {
"count": 5,
"rank": 3.7
}
}
},
"nbc.com": {
"disp": "#822176",
"data": {
"2015-02-08": {
"count": 3,
"rank": 5.5
},
"2015-02-23": {
"count": 5,
"rank": 8.4
},
"2015-03-14": {
"count": 7,
"rank": 4.7
}
}
}
}
}

在这种情况下如何明确指定类型参数?谢谢。

最佳答案

SelectMany 太多:

var t = new TData(); // your TData

var aa = t.data.SelectMany(x =>
x.Value.innerData.Select(y => new
{
url = x.Key,
disp = x.Value.disp,
date = y.Key,
count = y.Value.count,
rank = y.Value.rank,
}));

内部的必须是Select

关于c# - 如何使用 LINQ 展平类中的嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30942803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com