How to reduce same strings in a 'foreach'?

I'm new programming an have this issue that I couldn't get what i want to do, this is my code

foreach(RunePage rune in runePages)
{
    if(rune.Slots != null && rune.Slots.Count > 0)
    {
        foreach(RuneSlot runeSlot in rune.Slots)
        {
            var runeName = staticApi.GetRune(RiotSharp.Region.lan, runeSlot.RuneId,    RuneData.tags, Language.es_ES).Name;
            richTextBox1.Text = runeName + "\n" + richTextBox1.Text;
        }
     }
     richTextBox1.Text = rune.Name + "\n" + richTextBox1.Text;
}

Output:

AP

Greater Quintessence of Ability Power

Greater Quintessence of Ability Power

Greater Quintessence of Ability Power

Greater Glyph of Ability Power

Greater Glyph of Magic Resist

Greater Glyph of Ability Power

Greater Glyph of Magic Penetration

Greater Glyph of Magic Penetration

Greater Glyph of Magic Penetration

Greater Glyph of Magic Penetration

Greater Glyph of Magic Penetration

Greater Glyph of Magic Penetration

Greater Seal of Ability Power

Greater Seal of Ability Power

Greater Seal of Ability Power

Greater Seal of Armor

Greater Seal of Armor

Greater Seal of Armor

Greater Seal of Armor

Greater Seal of Armor

Greater Seal of Armor

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

Greater Mark of Magic Penetration

and all I want is something like this Output

AP

3x Greater Quintessence of Ability Power

2x Greater Glyph of Ability Power

1x Greater Glyph of Magic Resist

6x Greater Glyph of Magic Penetration

3x Greater Seal of Ability Power

6x Greater Seal of Armor

9x Greater Mark of Magic Penetration

How do I do this?

Jon Skeet
people
quotationmark

I would suggest using LINQ to group and count them:

// TODO: Build up the whole string, and set the Text property once.
// Oh, and rename richTextBox1 to something more descriptive.
foreach (RunePage rune in runePages)
{
    if (rune.Slots != null)
    {
        var grouped = rune.Slots
             .GroupBy(slot => slot.RuneId)
             .Select(group => new { Name = staticApi.GetRune(RiotSharp.Region.lan, 
                                                             group.Key, RuneData.tags, 
                                                             Language.es_ES).Name),
                                    Count = group.Count() })
             .Select(pair => string.Format("{0}x {1}", pair.Count, pair.Name));
        richTextBox1.Text = string.Join("\n", grouped);
    }
    richTextBox1.Text = rune.Name + "\n" + richTextBox1.Text;
}

people

See more on this question at Stackoverflow