从 C# 查找中获取值

发布于 2024-12-20 23:57:30 字数 781 浏览 1 评论 0原文

我对如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み青杉依旧 2024-12-27 23:57:30

这是因为查找项是一个集合。您可以像这样查看查找的每个值:

foreach (var myLookupItem in myLookup)
{
    Debug.WriteLine("Key: " + myLookupItem.Key);
    foreach (var myLookupValue in myLookupItem)
    {
        Debug.WriteLine("Value: " + myLookupValue);
    }
}

That's because the lookup item is a collection. You can see every value of the lookup like this:

foreach (var myLookupItem in myLookup)
{
    Debug.WriteLine("Key: " + myLookupItem.Key);
    foreach (var myLookupValue in myLookupItem)
    {
        Debug.WriteLine("Value: " + myLookupValue);
    }
}
来世叙缘 2024-12-27 23:57:30

“我认为它的含义与您认为的含义不同。”

查找(来自 MSDN):

表示一组键,每个键映射到一个或多个值。

即它将它们分组(即使这些组是单数)。

看起来您想使用字典之类的东西而不是查找。以字典为例。您可以使用 ToDictionary() 扩展方法而不是 ToLookup() 扩展方法。然后您可以使用字典上的键和值。

"I do not think it means what you think it means."

A Lookup (from MSDN):

Represents a collection of keys each mapped to one or more values.

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.

倦话 2024-12-27 23:57:30
int lookupValue = myLookup.Where(x => x.Key == "Test").SelectMany(x => x).FirstOrDefault())
int lookupValue = myLookup.Where(x => x.Key == "Test").SelectMany(x => x).FirstOrDefault())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文