【问题标题】:Dictionary to List of Keys on Conditional Match条件匹配键列表的字典
【发布时间】:2014-10-03 13:13:08
【问题描述】:

假设我有这本字典:

ConcurrentDictionary<string, Location> connections;

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

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

但这很讨厌,因为它从字典到子字典,再到键列表。有没有更优雅的方式来做到这一点?

【问题讨论】:

    标签: c# .net list dictionary type-conversion


    【解决方案1】:

    目前还不清楚您为什么接到ToDictionary 电话。你只需要一个Select 电话:

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

    【讨论】:

    • 度过了一个漫长的周末,伙计。我的大脑并非每天都能正常工作:) 谢谢,乔恩!
    猜你喜欢
    • 2021-12-11
    • 2022-10-14
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2021-07-09
    • 2021-11-17
    • 2011-09-24
    相关资源
    最近更新 更多