int cell = 1;
foreach (var item in resourceDictionary.Keys)
{
excelSheet.Cells[cell, 1] = item;
excelSheet.Cells[cell, 2] = resourceDictionary[item];
cell++;
}
在c#中使用linq有没有最简单的方法来实现这个目标?
答案 0 :(得分:8)
您的代码不需要LINQ。如果resourceDictionary
实现了IDictionary<TKey, TValue>
,那么以下内容可能会更具可读性/效率:
int cell = 1;
foreach (var item in resourceDictionary)
{
excelSheet.Cells[cell, 1] = item.Key;
excelSheet.Cells[cell, 2] = item.Value;
cell++;
}
答案 1 :(得分:3)
如果你想要它:
resourceDictionary.Select((p, i) => new {p.Key, p.Value, Cell = i + 1})
.ToList()
.ForEach(item => {
excelSheet.Cells[item.Cell, 1] = item.Key;
excelSheet.Cells[item.Cell, 2] = item.Value;
});