我有一個排序的字典,如下圖:如何將已排序字典輸出爲CSV?
var enteries = new SortedDictionary<IgaAdKey, IgaEntry>();
我想打印的項目爲0,則第2項,然後第4項,在一個行,CSV格式,然後去到項目1,3,5並以CSV格式打印出來。 但有些情況下,只有3個獨特的資源,或只有3個唯一的ResourceId
值和只有一個不同ResourceId
。 在這些情況下,我想通過寫入5個逗號(「,,,,,」)來打印空行。
這裏是我正在做,但當然我跳過新線
foreach (var pair in enteries.OrderBy(pair => pair.Key.ResourceId))
{
if (!currResources.Contains(pair.Key.ResourceId))
{
currResources.Add(pair.Key.ResourceId);
IgaEntry entry = enteries[pair.Key];
streamWriter.Write(pair.Key.LocationId + ",");
streamWriter.Write(pair.Key.EditionId + ",");
streamWriter.Write(entry.mClickCount + ",");
streamWriter.Write(entry.mViewCount + ",");
streamWriter.Write(",");
}
else
{
IgaEntry entry = enteries[pair.Key];
streamWriter.Write("\n");
streamWriter.Write(date.ToString("yyyyMMdd") + ",");
int search = resourcesList.IndexOf(pair.Key.ResourceId);
if (search > 0)
{
for (int i = 0; i < search * 5; i++)
{
streamWriter.Write(",");
}
}
streamWriter.Write(pair.Key.LocationId + ",");
streamWriter.Write(pair.Key.EditionId + ",");
streamWriter.Write(entry.mClickCount + ",");
streamWriter.Write(entry.mViewCount + ",");
streamWriter.Write(",");
}
}
}
代碼的結果是:
,它應該是這樣的:
如果什麼有超過3個資源具有相同'ResourceId'? – BartoszKP 2014-10-17 11:27:50