c# - 在字典中的数组内循环

标签 c#

您好,我有一个简单的问题,使用 C# 在字典中对象内的数组内循环的最简单方法是什么? 字典包含组和组有一个名为标签的数组,我搜索了一个标签并返回包含该标签的组列表 我创建了一个解决方案,但在我应用它时它返回了太多 double 值。

    List<Programme> toReturn = new List<Programme>();
        // might need to ask getprogramme service to do the iteriation and retun a value
        foreach (Programme programme in programmes.Values)
        {

            if (message.Programme.Tags[0] != null)
            {
                int i;
                int u;

                foreach (KeyValuePair<string, Programme> entry in programmes)
                {
                    // for (i = 0; i < message.Group.Tags.Length; i++)
                    for (i = 0; i < entry.Value.Tags.Length; i++)
                    //foreach (string i in message.Group.Tags)
                    {
                        for (u = 0; u < message.Programme.Tags.Length; u++)
                        {
                            // Compare the Name of the entry to the Name in the message (string comparison)
                            if (entry.Value.Tags[i].Equals(message.Programme.Tags[u]))
                            {
                                // If we found the group, set the return value and then break from the loop
                                toReturn.Add(programme);

                                break;
                            }


                        }
                    }
                }
            }

最佳答案

最简单的方法是使用 LINQ:

var res = groups.Where(g => g.Value.Any(t => t.Equals("search_tag")));

关于c# - 在字典中的数组内循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9879250/

相关文章:

c# - 'awaited'任务在哪里执行?

c# - Null 作为 HttpRuntime.Cache.Add 中的值

c# - C# 中的可滚动表单,AutoScroll=true 不起作用

c# - 安全 DSA 签名

c# - 如何从 Windows 窗体的组合框中隐藏特定项目

c# - Gridview 不保留 c# 和 ASP.Net 中的旧值

C#:按降序对字典进行排序

c# - 使用正则表达式防止 mvc 文本框中的 html 标签条目

c# - 为什么不能直接在外部调用公共(public)事件?

c# - C# 是否有在文件名的扩展部分之前插入字符串的方法?