.net - How to produce a fully unique lookup? -
let's have ienumerable
of key/value pairs, contains duplicates. if call tolookup
on , pass lambdas select key , value, i'll end lookup
keys unique, lists of values can contain duplicates. there way end lookup
none of lists of values contain duplicates, in single linq query statement?
i can't call distinct
on original sequence because elements duplicates value not object identity, , distinct
doesn't take lambda selector use.
you can use groupby before building lookup in combination select many. doesnt feel however:
something this:
sequence.groupby(kvp => _getkey(kvp)) .selectmany(grp => grp.distinct() .select(value => new { grp.key, value = value})) .tolookup(grp => grp.key, grp=> grp.value);
alternative using dictionary, since not quite sure why want use lookup.
sequence.groupby(kvp => _getkey(kvp)) .select(g => new { g.key, values = new hashset<whatever>(g)}) .todictionary(v => v.key, v => v.values);
alternative hashset providing equalitycomparer distinct()
public sealed class whatevercomparer : iequalitycomparer<whatever> { ... implement interface.. }
then
sequence.groupby(kvp => _getkey(kvp)) .select(g => new { g.key, values = g.distinct(new whatevercomparer()).toarray()}) .todictionary(v => v.key, v => v.values);
Comments
Post a Comment