c# - Why doesn't IGrouping inherit IEnumerable<KeyValuePair> like IDictionary? -
igrouping:
public interface igrouping<out tkey, out telement> : ienumerable<telement>, ienumerable
idictionary:
public interface idictionary<tkey, tvalue> : icollection<keyvaluepair<tkey, tvalue>>, ienumerable<keyvaluepair<tkey, tvalue>>, ienumerable
so, idictionary
implements ienumerable<keyvaluepair<tkey, tvalue>>
while igrouping
implements ienumerable<telement>
. if elements of igrouping
contain keys, why interface not use keyvaluepair
? seems methods implemented in idictionary
useful igrouping
such idictionary.containskey
unavailable in igrouping
, meaning attempt find key on group (in o(1) time) like:
list<int> mylist = new list<int>{ 1, 2, 3, 1}; var grp = mylist.groupby(x => x).todictionary(x => x.key, x => x.count()); if (grp.containskey(somevalue)){...}
am using igrouping
wrong? missing?
to find if particular igrouping<tkey, tvalue>
contains particular tkey
, check key
property directly. no need loop.
groupby
doesn't return igrouping
, returns ienumerable<igrouping<...>>
. is, igrouping
represents results 1 single key value, , multiple such results. cannot return dictionary<tkey, tvalue>
, groupby
preserves key order, , dictionary
doesn't. no other pre-existing collection type appropriate here.
since don't care key order, , don't care individual values each key (since they're identical), can store results in dictionary yourself, you're doing now. you're doing right thing.
if don't need counts, can use hashset<int>
.
if end needing individual values, can use tolookup
.
Comments
Post a Comment