我有一個文本文件的列表,我正在閱讀每個和拆分選項的幫助我創建一個數據表(名稱:tbl)與5列(無ID)。C# - 2字段DataTable列 - 獨特的值
此外,僅使用Column1和Column5創建數據表並將其轉換爲字典作爲column1將是一個鍵,column5將是其值。
以下是代碼:
string[] selectedColumns = new[] { "Column1", "Column5" };
DataTable dt = new DataView(tbl).ToTable(false, selectedColumns);
dict = dt.AsEnumerable().ToDictionary<DataRow, string, string>(row => row[0].ToString(),
row => row[1].ToString());
錯誤:{ 「已添加具有相同鍵的項」} $異常的System.Exception {System.ArgumentException}
由於重複而出現錯誤。
dict = dt.AsEnumerable().Distinct().ToDictionary<DataRow, string, string>(row => row[0].ToString(),
row => row[1].ToString());
結果相同。
dict = dt.AsEnumerable().ToDictionary<DataRow, string, string>(row => row[0].ToString(),
row => row[1].ToString()).Distinct();
拋出一個錯誤:錯誤1無法隱式轉換類型 'System.Collections.Generic.IEnumerable>' 到 'System.Collections.Generic.Dictionary'。存在明確的轉換(您是否缺少演員?)
如何避免重複並只將不同的值存入字典詞典?
在此先感謝。
你保證,在第1列的給定值將永遠在5列的值相同? – Chris