简体   繁体   English

如何使用 C# 中的扩展方法获取嵌套字典?

[英]How to get a nested dictionary using extension methods in C#?

I want to make a function that receives a csv file and returns a nested dictionary.我想做一个 function 接收 csv 文件并返回嵌套字典。 The csv file format looks like this(but there's no space between values): csv 文件格式如下所示(但值之间没有空格):

FilmMaker,   MovieTitle,      EndDate
FunnyM,      F1,              20191210
FunnyM,      F2,              20191211
FunnyM,      F3,              20191212
FunnyM,      F4,              20191213
FunnyM,      F5,              20191214
SadM,        S1,              20191210
SadM,        S2,              20191211
SadM,        S3,              20191212
SadM,        S4,              20191213
SadM,        S5,              20191214
ScaryM,      C1,              20191210
ScaryM,      C2,              20191211
ScaryM,      C3,              20191212
ScaryM,      C4,              20191213
ScaryM,      C5,              20191214

And this is the function I'm working on:这是我正在研究的 function:

public static Dictionary<string, Dictionary<string, string>> CSV_to_dictionary(String csvFileName)
{
    var data = File.ReadAllLines("movies.csv", Encoding.Default);

    var d = data.Skip(1)
                .Select(line => line.Split(',').ToList())
                .Select(a => new { FilmMaker= a[0], MovieTitle= a[1], EndDate= DateTime.ParseExact(a[2], "yyyyMMdd", CultureInfo.InvariantCulture) })
                .GroupBy(o => o.FilmMaker)
                .Select(g => new { FilmMaker= g.Key, info = g.Where(m => m.EndDate> new DateTime(2019, 12, 10)).OrderBy(m => m.EndDate).Skip(1).First() })
                .ToDictionary(m => m.info.MovieTitle,
                              m => new { FilmMaker= m.FilmMaker, EndDate= m.info.EndDate.ToString("yyyyMMdd") });

    return d;
}

The main goal is to extract one movie per FilmMaker on some conditions and make a nested dictionary like:主要目标是在某些条件下为每个 FilmMaker 提取一部电影,并制作一个嵌套字典,例如:

dictionary_movie = { "F3": { "FilmMaker":"FunnyM", "EndDate":"20191212" },
                     "S3": { "FilmMaker":"SadM",   "EndDate":"20191212" },
                     "C3": { "FilmMaker":"ScaryM", "EndDate":"20191212" } }

and refer to the dictionary in the main program.并参考主程序中的字典。

The problem is that the return type of the function isn't the same as the result of the extension methods.问题是 function 的返回类型与扩展方法的结果不同。 I thought it would be Dictionary<string, Dictionary<string, string>> , but it's Dictionary<string, <anonymous type: string, string>> .我以为是Dictionary<string, Dictionary<string, string>> ,但它是Dictionary<string, <anonymous type: string, string>> How can I change the anonymous type to the nested dictionary type?如何将匿名类型更改为嵌套字典类型? Is it possible to do it in a extension method?是否可以在扩展方法中做到这一点?

It looks like to me that you could replace this final line在我看来,您可以替换最后一行

m => new { FilmMaker= m.FilmMaker, EndDate= m.info.EndDate.ToString("yyyyMMdd") }

with this one有了这个

m => new Dictionary<string, string> { { "FilmMaker", m.FilmMaker }, { "EndDate", m.info.EndDate.ToString("yyyyMMdd") } }

and it's gonna create a new Dictionary<string, string> as the value instead of using an anonymous type.它会创建一个新的Dictionary<string, string>作为值,而不是使用匿名类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM