Autor Beitrag
Joey86
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 56



BeitragVerfasst: Do 24.06.10 18:47 
Hallo!
Gibt es eine einfache/schöne Möglichkeit zu zählen, wie häufig ein value (ein und derselbe Wert) innerhalb eines Dictionarys vorkommt?

Grüße


Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Do 24.06.2010 um 19:32
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 24.06.10 19:32 
Sollte über LINQ recht einfach gehen :-)

ausblenden C#-Quelltext
1:
myDict.Values.Where(aValue => aValue == "foo").Count();					

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



BeitragVerfasst: Do 24.06.10 20:00 
Zitat:
"System.Collections.Generic.Dictionary<string,string>.ValueCollection" enthält keine Definition für "Where", und es konnte keine Erweiterungsmethode "Where" gefunden werden, die ein erstes Argument vom Typ "System.Collections.Generic.Dictionary<string,string>.ValueCollection" akzeptiert. (Fehlt eine Using-Direktive oder ein Assemblyverweis?

Mhhh...Using-Direktive fehlt jedoch nicht.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 24.06.10 20:09 
Ich schrieb nicht umsonst, dass man LINQ benutzt. Das bietet im Namespace System.Linq eine Menge praktischer Extension Methods ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 24.06.10 20:15 
Du musst mind. auf .NET Framework 3.5 kompilieren.

Außerdem würde ich fürs zählen die Count-Methode verwenden:
ausblenden C#-Quelltext
1:
dict.Values.Count( item => item == object );					


Gruß
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 24.06.10 20:17 
user profile icondanielf hat folgendes geschrieben Zum zitierten Posting springen:
Du musst mind. auf .NET Framework 3.5 kompilieren.

Hat er zumindest angegeben, dass er das hat ;)

user profile icondanielf hat folgendes geschrieben Zum zitierten Posting springen:
Außerdem würde ich fürs zählen die Count-Methode verwenden:
ausblenden C#-Quelltext
1:
dict.Values.Count( item => item == object );					

Oh, die Überladung hatte ich übersehen, das ist dann natürlich besser. :-)

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



BeitragVerfasst: Do 24.06.10 20:22 
Okay, danke Jungs!
Fehler war, dass ich nicht System.Linq, sondern System.Data.Linq hatte.