group by - LINQ Get Grouped ID by condition -
hi have list so:
1 2 3 4 b 1 b 2 c 1
i want select letter contains @ least these 3 numbers: 1,2,3
so in case selected letter a.
can me write linq expression?
thanks lot!
first, make collection of numbers require.
var required = new[] { 1, 2, 3 };
then, group pairings letter.
var groupedpairings = pairings.groupby(p => letter, p => number);
then, discard pairings don't have 3 required items. (the logic here "take collection of required items, remove in group, , make sure there nothing left".)
var groupswithrequired = groupedpairings .where(g => !required.except(g).any());
now, if want letters, can do
var letterswithrequired = groupswithrequired.select(g => g.key);
or if want dictionary mapping letter collection of numbers, can do
var dictionary = groupswithrequired.todictionary(g => g.key, g => g.toarray()); var numbersfora = dictionary["a"]; // = {1, 2, 3, 4}
Comments
Post a Comment