int cell = 1;
foreach (var item in resourceDictionary.Keys)
{
excelSheet.Cells[cell, 1] = item;
excelSheet.Cells[cell, 2] = resourceDictionary[item];
cell++;
}
有没有最简单的方法可以在 c# 中使用 linq 来实现这一点?
您的代码不需要 LINQ。如果resourceDictionary
implements IDictionary<TKey, TValue>
,那么以下内容可能更具可读性/效率:
int cell = 1;
foreach (var item in resourceDictionary)
{
excelSheet.Cells[cell, 1] = item.Key;
excelSheet.Cells[cell, 2] = item.Value;
cell++;
}
如果你想要的话:
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;
});