不知道为什么这不起作用,它是从可以工作的代码改编而来。看起来像我需要一个明确的演员,但我不知道为什么或在哪里把它。错误是:CSV到字典的错误
“无法隐式转换类型 'System.Collections.Generic.IEnumerable {{System.Collections.Generic.Dictionary字符串,字符串}}' 到“System.Collections.Generic.Dictionary {串,串}'”
public static Dictionary<string, string> Data_Entry(string dataEntity, string dataCategory, string dataStream = "")
{
var lines = File.ReadAllLines(@"C:\MyFile.csv");
var header = lines.First().Split(',');
return (from line in lines.Skip(1)
let cols = line.Split(',')
where cols[0].ToUpper() == dataEntity & cols[1].ToUpper() == dataCategory & cols[4].ToUpper() == dataStream
select header.Select((h, i) => new { header = h, index = i })
.ToDictionary(o => o.header, o => cols[o.index])
);
}
这可能会帮助你解释这段代码试图做什么,确切地说。下面有几个答案正确地指出,你声明的返回类型和你的'return'语句是冲突的,但是如果不知道你的目标就很难超越它。 –
我想从结构化的CSV(只有一个匹配是可能的),通过where子句,以及标题行,标题值作为关键字和行值作为返回的字典中的值拉单匹配行。 – xnguyeng