[Question / help] Selectmany – my brain hurts

I needed to find two entities from a list whose attack SUM is equal or higher than a variable.

I googled and found this so I went ahead and updated it:

private static List<Entity> test(int intX, List<Entity> e) { var query = e.SelectMany((num1, j) => e.Select((num2, i) => new { n1 = num1, n2 = num2, i = i, j = j })).Where(x => x.n1.Attack + x.n2.Attack >= intX && x.i < x.j).OrderBy(x => x.n1.Attack + x.n2.Attack - intX); ... return etc etc } 

Now, it works perfectly, however in some cases the Attack of any two entities is not actually higher or equal than the intX, but if I took into account three, four, five or more entities, it would.

However, for the love of god I can’t wrap my head around updating the “function” for more than two entities.

Any help is appreciated.

submitted by /u/Seminko
[link] [comments]

Leave a Reply