字典第一个条目 [英] Dictionary First Entry

查看:94
本文介绍了字典第一个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想得到第一个没有钥匙的字典条目.我知道我可以设置一个foreach KeyValuePair循环并在第一次迭代后中断,但是必须有更好的方法.有什么想法吗?

我要执行此操作的原因是该词典包含应用程序名称和电子表格上的相应行.字典中的项目按行顺序添加.我需要获取第1行编号才能在其上方添加标题.加载到字典中的数据来自一个文件,并且对于每个电子表格而言都是不同的,但是所有这些逻辑都适用相同的逻辑.

谢谢您,

Hi,

I''d like to get the 1st dictionary entry without a key. I know I can setup a foreach KeyValuePair loop and break out after the 1st iteration, but there must be a better way. Any ideas?

The reason I want to do this is the dictionary contains application names and corresponding rows on a spreadsheet. The items in the dictionary are added in row order. I need to get the 1st row number in order to add a heading right above it. The data loaded into the dictionary comes from a file and is different for each spreadsheet, however the same logic applies to all of them.

Thank you,

推荐答案

为什么要第一个.在字典中,键确定值,并且它可以处于任何级别.第一个键有什么特别之处.能否使用以键为输入的TryGetValue().请参见此处 [
Why the first. In dictionary the key determines the values and it can be at any level. What is special about the first key. Can you use TryGetValue() which takes key as input. See here[^]


您可以使用FirstFirstOrDefault扩展方法:
You can use the First or FirstOrDefault extension methods:
Dictionary<object, object> dict = new Dictionary<object, object>();
// dict.Add(new object(), new object()); // optional
object firstObj = dict.FirstOrDefault().Value;


但是就像优素福所说,为什么?字典是无序的,因此它们实际上没有第一个"或最后一个"元素的概念.

IEnumerable.First [ IEnumerable.FirstOrDefault [


but like Yusuf said, why? Dictionaries are unordered so they don''t really have a concept of a "first" or "last" element.

IEnumerable.First[^]
IEnumerable.FirstOrDefault[^]


这篇关于字典第一个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆