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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -