Group objects by a series of common IDs and select a grouping based on the sum of a different key in LINQ -


so have bit of academic question. able solve using brute force approach, believe solvable more elegantly in linq - however, can't find way so. solution can c# or vb, it's linq giving me trouble.

i have following object:

public class foo {     public int fooid {get; set; }      public int bar {get; set; }      /// <summary>     /// contains list of fooid     /// </summary>     public int[] stackability } 

what need find possible groupings of foos based on stackability (i.e.: common fooid in stackability list), select grouping has highest sum of bar.

so, example: have foos ids of 1, 2 , 3.

+-------+-----+--------------+   | fooid | bar | stackability |   +-------+-----+--------------+   | 1     | 5   | 2, 3         |   +-------+-----+--------------+   | 2     | 2   | 1, 3         |   +-------+-----+--------------+   | 3     | 6   | 1            |   +-------+-----+--------------+   
  • foo 1's stackability contains values of 2 , 3 , has bar of 5.
  • foo 2's stackability contains values of 1 , 3 , has bar of 2.
  • foo 3's stackability contains value of 1 , has bar of 6.

in example, foo 1 can stack foo 2 , foo 3, foo 2 can stack foo 1 , foo 3, , foo 3 can stack foo 1.

note stacking has reciprocal (so though foo 2 can stack foo 1 , foo 3, foo 3 can stack foo 1, means foo 2 can stack foo 1).

this give following groupings corresponding sums bar:

  • { foo 1, foo 2 } - bar == 7
  • { foo 1, foo 3 } - bar == 11
  • { foo 1 } - bar == 5
  • { foo 2 } - bar == 2
  • { foo 3 } - bar == 6

in case, correct grouping foo 1 , foo 3.

thanks in advance!


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -