从 C# 查找中获取值
我对如何从 C# 查找结构中获取价值感兴趣。
示例:
var myLookup = (Lookup<string, int>)data.Rows.Cast<DataRow>().ToLookup(row => row["Name"], row => row["Id"]);
foreach (var myLookupItem in myLookup)
{
Debug.WriteLine("Name: " + myLookupItem.Key);
Debug.WriteLine("Id: " + myLookupItem.ToString());
}
问题是
myLookupItem.ToString()
不显示实际值,而是仅显示 System.Linq.Lookup2[System.String,System.Int32]
。
稍后,我应该使用 lambda: 获取查找值:
int lookupValue = myLookup.Where(x => x.Key == "Test").Select(x => x).FirstOrDefault());
但这也给出与上面相同的结果。
请告知如何实现这一目标。
提前致谢。
I'm interested in how to get value from C# lookup structure.
Example:
var myLookup = (Lookup<string, int>)data.Rows.Cast<DataRow>().ToLookup(row => row["Name"], row => row["Id"]);
foreach (var myLookupItem in myLookup)
{
Debug.WriteLine("Name: " + myLookupItem.Key);
Debug.WriteLine("Id: " + myLookupItem.ToString());
}
Problem is that the
myLookupItem.ToString()
doesn't display actual value, instead only System.Linq.Lookup2[System.String,System.Int32]
is displayed.
Later on, I should get the lookup value using lambda:
int lookupValue = myLookup.Where(x => x.Key == "Test").Select(x => x).FirstOrDefault());
but this also gives the same as above.
Please advise how to achieve this.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为查找项是一个集合。您可以像这样查看查找的每个值:
That's because the lookup item is a collection. You can see every value of the lookup like this:
“我认为它的含义与您认为的含义不同。”
查找(来自 MSDN):
即它将它们分组(即使这些组是单数)。
看起来您想使用字典之类的东西而不是查找。以字典为例。您可以使用 ToDictionary() 扩展方法而不是 ToLookup() 扩展方法。然后您可以使用字典上的键和值。
"I do not think it means what you think it means."
A Lookup (from MSDN):
i.e. it groups them (even if these groups are singular).
It looks like you want to use something like a Dictionary instead of a Lookup. In the case of a Dictionary. You can use the ToDictionary() extension method instead of the ToLookup() extension method. You'd then use key and value on the dictionary.