Autor Beitrag
Help_Me
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: Sa 24.04.10 16:46 
Hi!
Aufgrund eines Verbesserungsvorschlags hab ich meinen Code verändert und zum ersten Mal mit einem Dictionary gearbeitet. Allerdings häng ich jetzt noch an etwas. Hier mal der Code:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
Dictionary<TypeCode, int> countDic = new Dictionary<TypeCode, int>();

TypeCode code = Type.GetTypeCode(werte.GetValue(i, j).GetType());
if (! countDic.ContainsKey(code))
{
     countDic.Add(code, 0);
}
++countDic[code];

Von jedem Wert, den ich zurück bekomme, prüfe ich den Typ. Gleiche Typen werden addiert.
Nur jetzt hänge ich gerade. Wie bekomme ich elegant den am häufigsten vorkommenden Typen?

Gruß
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 24.04.10 17:07 
Ein Dictionary implementiert IEnumerable<>. Du kannst mit einer foreach-Schleife durchgehen und den jeweils bisher größten Wert in einer Variablen speichern.

Wenn dir LINQ allerdings schon etwas sagt, gibt es keinen Grund, hier das Rad neu zu erfinden. Mit GroupBy und Max solltest du in zwei Zeilen am Ziel sein :) .

_________________
>λ=
Help_Me Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: Sa 24.04.10 18:19 
Ahhh...ich habs noch immer nicht, sorry.
Hab jetzt mir mal paar Beispiele zu LINQ angeschaut und versucht, auf meinen
Code zu übertragen, aber es läuft noch immer nicht...
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Sa 24.04.10 19:58 
Hmpf, die naheliegenden Methoden haben die LINQ-Macher natürlich vergessen. Ich dachte an so etwas:
ausblenden C#-Quelltext
1:
2:
3:
deineWerteAlsIEnumerable.Select(w => Type.GetTypeCode(w.GetType()))
.GroupBy(type => type)
.MaxBy(group => group.Count()) // http://code.google.com/p/morelinq/source/browse/trunk/MoreLinq/MaxBy.cs


Ich gehe mal davon aus, dass wir lieber den anderen Weg wählen ;) . Hast du es schon einmal mit foreach versucht?

_________________
>λ=
Help_Me Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: So 25.04.10 10:57 
Moin!
Habs jetzt mal so gelöst:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
        TypeCode theKey = default(TypeCode);
        int maxVal = int.MinValue;
        foreach (KeyValuePair<TypeCode, int> cur in countDic) 
        {                      
            int curVal = Convert.ToInt32(cur.Value);
            if (curVal > maxVal)
            {
                maxVal = curVal;
                theKey = cur.Key;
            }
        }

Scheint zu funktionieren. Für Verbesserungsvorschläge bin ich aber offen. :wink:
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 25.04.10 11:04 
cur.Value ist schon ein int, das Convert ist also unnötig. Ansonsten erscheint mir das so gut zu sein.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Help_Me Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: So 25.04.10 11:32 
Okay...danke!
Help_Me Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: So 25.04.10 13:12 
Jetzt hab ich doch noch eine Kleinigkeit...
Ich wollte prüfen, ob es sich bei dem Key um einen Key vom Typ DBNull handelt,
diesen würde ich dann nicht mitzählen wollen.
Gedacht hatte ich mir das so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
            TypeCode theKey = default(TypeCode);
            int maxVal = int.MinValue;
            foreach (KeyValuePair<TypeCode, int> cur in countDic) 
            {
                int curVal = cur.Value;
                if (curVal > maxVal)
                {
                    if (!(cur.Key is DBNull))
                    {
                        maxVal = curVal;
                        theKey = cur.Key;
                    }
                }
            }

Nur ist cur.Key nie vom Typ DBNull. Warum? :gruebel:
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 25.04.10 13:56 
user profile iconHelp_Me hat folgendes geschrieben Zum zitierten Posting springen:
Nur ist cur.Key nie vom Typ DBNull. Warum?
Weil es vom Typ TypeCode ist ;) .
ausblenden C#-Quelltext
1:
if (curVal > maxVal && cur.Key != TypeCode.DBNull)					

_________________
>λ=
Help_Me Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 31



BeitragVerfasst: So 25.04.10 14:14 
Aaahhh...da könnt ich mir in den Hintern beißen.
Ich hatte auch die Prüfung mit TypeCode drin, allerdings immer noch den Operator is.

Danke!