0
中的第一個的Linq Key必須將列表投遞到我的應用程序中的字典,但即時通訊出現錯誤,指出「具有相同密鑰的項目已被添加」。 我需要第一密鑰和他的值只包含每個重複
Dictionary<string, string> cells =
(from cell in sheet.Cells["A1:J20"]
where cell.Start.Column == 1 && cell.Value != null
select sheet.Cells[cell.Start.Row, 1, cell.Start.Row,9].Value)
.Cast<object[,]>()
.Distinct().ToDictionary(k => Convert.ToString(k[0, 2]), v =>
Convert.ToString((v[0, 8])));
例Excel中:
鍵 =>值
鍵1 =>值1
- 密鑰2 => Value2
- 密鑰3 =>值3
- 密鑰3 =>值4
- 密鑰3 =>值5
- 鍵6 => Value6
- KEY7 => Value7
- Key23 => Value8
EDIT
Dictionary<string, string> dict = new Dictionary<string, string>();
var cells = (from cell in sheet.Cells["A1:B16"]
where cell.Start.Column == 1 && cell.Value != null
select sheet.Cells[cell.Start.Row, cell.Start.Column, cell.Start.Row, 2].Value)
.Cast<object[,]>();
週期,並添加到字典:
foreach (var i in cells) {
if (dict.ContainsKey(Convert.ToString(i[0, 0])) == false)
dict.Add(Convert.ToString(i[0, 0]), Convert.ToString(i[0, 1]));
// dict.Distinct();
}
但我需要在LINQ代碼!
很抱歉,但代碼有錯誤( –
你什麼錯誤? – NetMage
我寫新代碼!請你在使用獲得LINQ到sheet.Cells [幫我 –