数组到字典

发布于 2024-10-22 18:09:09 字数 450 浏览 3 评论 0原文

我试图将此 _valueAdds = List 放入 gridItemsDictionary),其中 _valueAdds 为键和所有值均为 false。但我不知道如何用 Lamda 做到这一点。这就是我在下面走了多远。我确实成功地用 while 循环做到了这一点,但想学习用 Lamda 做到这一点

gridItems = new Dictionary<ValueAddedItemHelper, bool>();
gridItems = _valueAdds.Select(k => new { k }).ToArray().ToDictionary(t => t, false);

I'm trying to get this _valueAdds = List<ValueAddedItemHelper> into gridItems (Dictionary) with _valueAdds being the Key and all the values being false. But I'm not sure how to do this with Lamda. This how far I got below. I did succeed in doing it with a while loop but would like learn to do it with Lamda

gridItems = new Dictionary<ValueAddedItemHelper, bool>();
gridItems = _valueAdds.Select(k => new { k }).ToArray().ToDictionary(t => t, false);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

我的影子我的梦 2024-10-29 18:09:09
_valueAdds.ToDictionary(t => t, t => false);
_valueAdds.ToDictionary(t => t, t => false);
盛装女皇 2024-10-29 18:09:09

您需要提供 lambda 表达式作为第二个参数(或以其他方式创建委托,但 lambda 表达式是最简单的)。请注意,对 ToArray 的调用不是必需的,您要创建的空字典也不是必需的。只是使用:

gridItems = _valueAdds.Select(k => new { k })
                      .ToDictionary(t => t, t => false);

我不清楚为什么你在这里使用匿名类型......特别是,不会是一个ValueAddedItemHelper。您是否需要投影?也许只是:

gridItems = _valueAdds.ToDictionary(t => t, t => false);

You need to provide a lambda expression as the second argument (or create the delegate some other way, but a lambda expression will be simplest). Note that the call to ToArray isn't required, and nor is the empty dictionary you're creating to start with. Just use:

gridItems = _valueAdds.Select(k => new { k })
                      .ToDictionary(t => t, t => false);

It's not clear to me why you're using an anonymous type here though... in particular, that won't be a ValueAddedItemHelper. Do you need the projection at all? Perhaps just:

gridItems = _valueAdds.ToDictionary(t => t, t => false);
洛阳烟雨空心柳 2024-10-29 18:09:09

您不需要 ToArray().ToDictionary()。您只需执行 ToDictionary() 即可。而且你不需要第一行。第二行将创建并填充字典。

代码:

gridItems = _valueAdds.ToDictionary(p => p, p => false);

You don't need a ToArray().ToDictionary(). You can simply do a ToDictionary(). And you don't need the first line. The second line will create and fill the dictionary.

The code:

gridItems = _valueAdds.ToDictionary(p => p, p => false);
红尘作伴 2024-10-29 18:09:09

假设 _valueAddsIEnumerable 你可以这样做:

gridItems = _valueAdds.ToDictionary(x => x, x => false);

Assuming _valueAdds is an IEnumerable<ValueAddedItemHelper> you could do this:

gridItems = _valueAdds.ToDictionary(x => x, x => false);
顾冷 2024-10-29 18:09:09

像这样的东西

var gridItems = _valueAdds.ToDictionary(k=>k,i=>false);

Something like

var gridItems = _valueAdds.ToDictionary(k=>k,i=>false);
木槿暧夏七纪年 2024-10-29 18:09:09

Select(k => new { k }) 是问题所在;它创建一个带有名为 k 的属性的匿名类型。只是:

var gridItems = _valueAdds.ToDictionary(t => t, t => false);

The Select(k => new { k }) is the problem; that creates an anonymous type with a property called k. Just:

var gridItems = _valueAdds.ToDictionary(t => t, t => false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文