Loop inside an array in a dictionary

Hi I have a quick question, which is the simplest way to loop inside an array that is inside an object in a dictionary using C #? The dictionary contains groups and the groups have an array called tags, I search for a tag and return a list of groups that contain that tag I created a solution but it returns too many doubles when applied.

    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;
                            }


                        }
                    }
                }
            }

      

+3


source to share


1 answer


The easiest way is to use LINQ:



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

      

+2


source







All Articles