订购词典
我使用以下代码将信息添加到字典中:
foreach (string word in lineList)
{
if (dictionary.ContainsKey(word))
dictionary[word]++;
else
dictionary[word] = 1;
}
// I believe this is what needs to change..?
var ordered = from k in dictionary.Keys select k;
当我使用 StreamWriter 打印行时,它会按照添加到字典中的顺序打印出来。
我想做的是按照首先比较 PartDescription
然后比较 PartNumber
的顺序打印出来,然后以数字方式打印出来。
文件如下所示:
PartDescription PartNumber Name X Y Rotation
1608RTANT 147430 J1 20.555 -12.121 180
TANTD 148966 J2 20.555 -12.121 270
SOMETHING 148966 R111 20.555 -12.121 360
SOMETHING 148966 C121 20.555 -12.121 180
SOMETHING 148966 R50 205.555 -12.121 180
SOMETHING 148966 R51 -205.555 125.121 270
SOMETHING 148966 R52 20.555 -12.121 0
SOMETHING 148966 C12 20.555 -12.121 0
1709RTANT 147430 C98 20.555 -12.121 0
1608RTANT 147429 QD1 20.555 -12.121 180
1709RTANT 147430 F12 20.555 -12.121 0
1609RTANT 147445 P9 20.555 -12.121 180
StreamWriter
将输出如下:
1, 1608RTANT, 147429, 1 //Line#, PartDescription, PartNumber, # of Duplicates (from dictionary key)
2, 1608RTANT, 147430, 1
3, 1609RTANT, 147445, 1
4, 1709RTANT, 147430, 2
5, SOMETHING, 148966, 6
6, TANTD, 148966, 1
I am adding information to a dictionary using this code:
foreach (string word in lineList)
{
if (dictionary.ContainsKey(word))
dictionary[word]++;
else
dictionary[word] = 1;
}
// I believe this is what needs to change..?
var ordered = from k in dictionary.Keys select k;
When I use the StreamWriter
to print out the lines it is printing it out in the order it was added to the dictionary
.
What I am trying to do is print it out in an order that first compares the PartDescription
and then the PartNumber
and prints it out numerically.
the File looks like this:
PartDescription PartNumber Name X Y Rotation
1608RTANT 147430 J1 20.555 -12.121 180
TANTD 148966 J2 20.555 -12.121 270
SOMETHING 148966 R111 20.555 -12.121 360
SOMETHING 148966 C121 20.555 -12.121 180
SOMETHING 148966 R50 205.555 -12.121 180
SOMETHING 148966 R51 -205.555 125.121 270
SOMETHING 148966 R52 20.555 -12.121 0
SOMETHING 148966 C12 20.555 -12.121 0
1709RTANT 147430 C98 20.555 -12.121 0
1608RTANT 147429 QD1 20.555 -12.121 180
1709RTANT 147430 F12 20.555 -12.121 0
1609RTANT 147445 P9 20.555 -12.121 180
The StreamWriter
would output like this:
1, 1608RTANT, 147429, 1 //Line#, PartDescription, PartNumber, # of Duplicates (from dictionary key)
2, 1608RTANT, 147430, 1
3, 1609RTANT, 147445, 1
4, 1709RTANT, 147430, 2
5, SOMETHING, 148966, 6
6, TANTD, 148966, 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你当然可以轻松地以有序的方式获取键:
或者更简单:
请注意,你不应该依赖字典按照你添加它们的顺序存储对 - 基本上你应该不假设字典中有任何排序。
Well you could certainly get the keys in an ordered way easily:
Or even more simply:
Note that you shouldn't rely on the dictionary storing the pairs in the order in which you added them - basically you shouldn't assume any ordering out of the dictionary.