C# show string once with amount in numbers of the selection in Foreach loop

116 Views Asked by At

So i am making a mailfunction and im having an issue showing the data the way i want it to show.

This is my code(editted to show only the issue im having):

public string Formatsfunction(List<Metadata> mmds)
{
    Dictionary<string, int> formatNumber = new Dictionary<string, int>();

    foreach (Metadata mmd in mmds)
    {
        var type = mmd.Format.Type;
        var found = formatNumber.ContainsKey(type);

        if (found == true)
        {
            formatNumber[type]++;
        }
        else
        {
            formatNumber[type] = 1;
        }
    }

    Console.WriteLine();

    return null;
}

private string MeldingInformatie(string impact, string type, List<Metadata> mmds)
{
    var temp = Formatsfunction(mmds);
    var formats = mmds.Select(mmd => mmd.Format.Type +"("+temp+")");
    ...
}

What i want it to do is:

If there are multiple of the same types in the Dictionary write it like:

Lion(3) Bear(2) Fish(8) Koala(12) etc.

I think i already got a good start going but am not sure what to do next.

4

There are 4 best solutions below

0
On

Thanks for the help guys! Combining all the knowledge you guys have given me i fixed the problem. For those interested the answer was:

  public string Formatsfunction(List<Metadata> mmds)
    {
       Dictionary<string, int> formatNumber = new Dictionary<string, int>();
        foreach (Metadata mmd in mmds)
        {
            var type = mmd.Format.Type;
            var found = formatNumber.ContainsKey(type);
            if (found == true)
            {
                formatNumber[type]++;

            }
            else
            {
                formatNumber[type] = 1;
            }
        }
        var temp = formatNumber
            .OrderByDescending(p=>p.Value)
            .Select(mmd => mmd.Key +" ("+ mmd.Value+")");
        string keyvalues = string.Join(", ", temp);
        return keyvalues;
    }
    private string MeldingInformatie(string impact, string type, List<Metadata> mmds)
    {
        var temp = Formatsfunction(mmds);

        var formats = mmds.Select(mmd => mmd.Format.Type +"("+temp+")");
        var trackTrace = mmds.Select(mmd => mmd.TrackTrace);
        string messageTypes = string.Join(", ", formats);
     }

Output is now: Lion(8), Fish(7), Koala(6), Seagull(5), etc.
4
On

Try following :

            Dictionary<string, int> formatNumber = new Dictionary<string, int>() {
                {"Lion",3},{"Bear",2},{ "Fish",8}, {"Koala", 12}};

            Console.WriteLine(string.Join(" ", formatNumber.AsEnumerable().Select(x => string.Format("{0}({1})", x.Key, x.Value)).ToList()));
0
On

You're probably looking for this:

Returning null is very useless. You need to return the dictionary and use that to format the string. Just select the keys (which are the types) and format it with the value. Use string.Join to combine all types to one string.

public Dictionary<string, int> Formatsfunction(List<Metadata> mmds)
{
    Dictionary<string, int> formatNumber = new Dictionary<string, int>();
    foreach (Metadata mmd in mmds)
    {
        var type = mmd.Format.Type;
        var found = formatNumber.ContainsKey(type);
        if (found == true)
        {
            formatNumber[type]++;

        }
        else
        {
            formatNumber[type] = 1;
        }
    }
    Console.WriteLine();

    return formatNumber;
}
private string MeldingInformatie(string impact, string type, List<Metadata> mmds)
{
    var temp = Formatsfunction(mmds);

    // (String interpolation is used)
    var formats = temp.Keys.Select(type => $"{type}({temp[type]})");

    return String.Join(" ", formats);

What does impact do? It might not solve all the problems, but might point you in the right direction.

2
On

Using LINQ's GroupBy() you can write the whole method like:

public string Formatsfunction(List<Metadata> mmds)
{
    return string.Join(" ", 
               mmds.GroupBy(mmd => mmd.Format.Type, (type, g) => $"{type}({g.Count()})"));
}

This groups the elements in mmds by their Format.Type properties and creates a string containing Type(count) pairs.