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 [帮我 –