gpt4 book ai didi

c# - 字典到条件匹配的键列表

转载 作者:太空宇宙 更新时间:2023-11-03 10:41:23 25 4
gpt4 key购买 nike

假设我有这本字典:

ConcurrentDictionary<string, Location> connections;

我想从这个字典中获取一个键列表,这些键与它们的值的某些条件相匹配。例如,这对我有用:

List<string> users = connections.Where(x.Value.IsNearby(anotherLocation)).ToDictionary(x => x.Key, x => x.Value).Keys.ToList();

但这很讨厌,因为它从字典到子字典,再到键列表。有更优雅的方法吗?

最佳答案

根本不清楚为什么要调用 ToDictionary。您只需要一个 Select 调用:

List<string> users = connections.Where(x => x.Value.IsNearby(anotherLocation)
.Select(x => x.Key)
.ToList();

关于c# - 字典到条件匹配的键列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25230898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com