通过键从字典中获取项目

发布于 2024-12-05 23:04:51 字数 499 浏览 1 评论 0原文

我有这样的结构:

static Dictionary<int, Dictionary<int, string>> tasks = 
    new Dictionary<int, Dictionary<int, string>>();

看起来

[1]([8] => "str1")
[3]([8] => "str2")
[2]([6] => "str3")
[5]([6] => "str4")

我想从这个列表中获取所有 [8] 字符串,意思是 str1 + str2< br> 该方法应如下所示:

static List<string> getTasksByNum(int num){

}

如何访问它?

I have this structure:

static Dictionary<int, Dictionary<int, string>> tasks = 
    new Dictionary<int, Dictionary<int, string>>();

it looks like that

[1]([8] => "str1")
[3]([8] => "str2")
[2]([6] => "str3")
[5]([6] => "str4")

I want to get from this list all of the [8] strings, meaning str1 + str2
The method should look like the following:

static List<string> getTasksByNum(int num){

}

How do I access it?

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

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

发布评论

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

评论(7

愛放△進行李 2024-12-12 23:04:52

丹尼尔的解决方案可能是最好的,因为它更容易理解。但也可以在 linq 方法中使用 TryGetValue:

return tasks.Values
    .Select(dictionary => {
        string task;
        var success = dictionary.TryGetValue(yourKey, out task);
        return new { success, task };
    })
    .Where(t => t.success)
    .Select(t => t.task)
    .ToList();

Daniel's solution is probably best, since it's easier to understand. But it's possible to use TryGetValue in a linq approach, too:

return tasks.Values
    .Select(dictionary => {
        string task;
        var success = dictionary.TryGetValue(yourKey, out task);
        return new { success, task };
    })
    .Where(t => t.success)
    .Select(t => t.task)
    .ToList();
烏雲後面有陽光 2024-12-12 23:04:52

您正在构建任务吗?
如果我猜对了,那就是tasks[task_id]([cpu] => "task_name");
我建议您还构建 cpu_tasks[cpu]([task_id] => "task_name);

static Dictionary<int, Dictionary<int, string>> cpu_tasks

它需要更多的维护,但会让您更快地运行这个特定的函数。

Are you building tasks ?
And if I'm guessing right it's tasks[task_id]([cpu] => "task_name");
I would advice you also build cpu_tasks[cpu]([task_id] => "task_name);

static Dictionary<int, Dictionary<int, string>> cpu_tasks

It would require some more maintenance but would give you a faster run on this specific function.

柏拉图鍀咏恒 2024-12-12 23:04:52
Dictionary<int, Dictionary<int, string>> tasks = new Dictionary<int, Dictionary<int, string>>();

List<string> strings = new List<string>();
foreach(var dict in tasks.Values)
{
  if(dict.ContainsKey(8))
      strings.Add(dict[8]);
}
Dictionary<int, Dictionary<int, string>> tasks = new Dictionary<int, Dictionary<int, string>>();

List<string> strings = new List<string>();
foreach(var dict in tasks.Values)
{
  if(dict.ContainsKey(8))
      strings.Add(dict[8]);
}
诗酒趁年少 2024-12-12 23:04:52
Dictionary<int, Dictionary<int, string>> tasks = new Dictionary<int, Dictionary<int, string>>();
var result = string.Empty;

//more human-readable version
var searchValue = 8;
foreach (var task in tasks)
{
     if (task.Value.ContainsKey(searchValue))
         result += task.Value[searchValue];
}

//one-line version
result = tasks.ToList().Aggregate(string.Empty, (a, kvp) => a += kvp.Value.ContainsKey(searchValue) ? kvp.Value[searchValue] : string.Empty);
Dictionary<int, Dictionary<int, string>> tasks = new Dictionary<int, Dictionary<int, string>>();
var result = string.Empty;

//more human-readable version
var searchValue = 8;
foreach (var task in tasks)
{
     if (task.Value.ContainsKey(searchValue))
         result += task.Value[searchValue];
}

//one-line version
result = tasks.ToList().Aggregate(string.Empty, (a, kvp) => a += kvp.Value.ContainsKey(searchValue) ? kvp.Value[searchValue] : string.Empty);
温折酒 2024-12-12 23:04:51

使用 LINQ,您可以执行以下操作:

return tasks.Values
            .Where(dict => dict.ContainsKey(8))
            .Select(dict => dict[8])
            .ToList();      

虽然这很优雅,但 TryGetValue 模式通常比它使用的两个查找操作更可取(首先尝试 ContainsKey,然后使用索引器来获取值)。

如果这对您来说是个问题,您可以执行类似的操作(使用合适的辅助方法):

return tasks.Values
            .Select(dict => dict.TryGetValueToTuple(8))
            .Where(tuple => tuple.Item1)
            .Select(tuple => tuple.Item2)
            .ToList();  

With LINQ, you can do something like:

return tasks.Values
            .Where(dict => dict.ContainsKey(8))
            .Select(dict => dict[8])
            .ToList();      

While this is elegant, the TryGetValue pattern is normally preferable to the two lookup operations this uses (first trying ContainsKey and then using the indexer to get the value).

If that's an issue for you, you could do something like (with a suitable helper method):

return tasks.Values
            .Select(dict => dict.TryGetValueToTuple(8))
            .Where(tuple => tuple.Item1)
            .Select(tuple => tuple.Item2)
            .ToList();  
温馨耳语 2024-12-12 23:04:51

只需迭代第一个层次结构级别的所有值,并在第二个级别上使用 TryGetValue

var result = new List<string>();
foreach(var inner in tasks.Values)
{
    string tmp;
    if(inner.TryGetValue(yourKey, out tmp)
        result.Add(tmp);
}

此解决方案比迄今为止提出的所有其他解决方案具有主要优势:
它实际上使用第二层次结构级别的字典作为字典,即 foreach 循环内的部分是 O(1) 而不是所有其他解决方案的 O(n)。

Just iterate over all values of the first hierarchy level and use TryGetValue on the second level:

var result = new List<string>();
foreach(var inner in tasks.Values)
{
    string tmp;
    if(inner.TryGetValue(yourKey, out tmp)
        result.Add(tmp);
}

This solution has a major advantage over all other solutions presented so far:
It actually uses the dictionaries of the second hierarchy level as a dictionary, i.e. the part inside the foreach loop is O(1) instead of O(n) as with all other solutions.

终止放荡 2024-12-12 23:04:51

检查这个功能:

   tasks.
      Where(task => task.Value.ContainsKey(8)).
      Select(task => task.Value[8]);

Check this function:

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