c# - Group by array contents -
i have list<tuple<string,long,byte[]>>
, want group contents of byte array.
is there simple way groupby
, lambda?
ideally, want without creating intermediate data structure (like string hold elements of array).
you can achieve using custom iequalitycomparer<byte[]>
(or better, generic one: iequalitycomparer<t[]>
) implementation:
class arraycomparer<t> : iequalitycomparer<t[]> { public bool equals(t[] x, t[] y) { return x.sequenceequal(y); } public int gethashcode(t[] obj) { return obj.aggregate(string.empty, (s, i) => s + i.gethashcode(), s => s.gethashcode()); } }
i'm pretty sure gethashcode
implemented better, it's example!
usage:
var grouped = source.groupby(i => i.item3, new arraycomparer<byte>())
Comments
Post a Comment