不知道爲什麼這不起作用,它是從可以工作的代碼改編而來。看起來像我需要一個明確的演員,但我不知道爲什麼或在哪裏把它。錯誤是: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