c# - Counting the amount of instances of a certain string in a list of objects, then formatting it in a certain way -
i have list of objects contain string. when enumerate through them, want able identify matching ones , group them together, format them string so: a005(x3), b006(x5), c002(x7),d001(x9).
can shed light on way this?
this code should give desired result assuming have collection of strings.
strlist.groupby(x => x) .select(x => string.format("{0}(x{1})", x.key, x.count())); here test program:
var values = new[] {"a005", "b006", "c002", "d001", "b006", "a005", "d001", "c002", "a005" }; var uniquevalues = values .groupby(x => x) .select(x => string.format("{0}(x{1})", x.key, x.count())); console.writeline(string.join(", ", uniquevalues)); produces output:
a005(x3), b006(x2), c002(x2), d001(x2)
Comments
Post a Comment