How to flatten nested dictionaries within Class using LINQ
最接近我所寻找的解决方案是这个线程如何用LINQ表达式扁平嵌套对象
但是我在尝试这种方法时出错了
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.
我的代码:
1 2 3 4 5 6 7 8 | 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, })); |
我的班级:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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; } } |
我从第三方API获得的JSON如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | { "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 } } } } } |
在这种情况下,如何显式指定类型参数?谢谢。
1 2 3 4 5 6 7 8 9 10 11 |
内部必须是一个