LINQ - Select rows based on whether their sum meets a condition

@sonnymeyer

the sum of all quantities will allways the same one sum.
So Grouping will not lead to a set of combinations between rows. I didnt check in detail but I would expect 1 group with an amount of 45 with 5 group members

What I understand from your requirements (I will it elaborate on a set combinations of 2)

  • combine each row with an additona onel to a pair like 1-10 with 1-10, 2-5,3-5… etc
    • So we got a carteasian product n x n rows
  • sum up the Quantity of this pairs
  • filter the sum on 20
  • dump out which groups it will be

In LINQ:
(From a In dtData.AsEnumerable
From b In dtData.AsEnumerable
Where Not a(0).ToString.equals(b(0).toString)
Let s = Convert.ToDouble(a(“Quantity”), cultureInfo) + Convert.ToDouble(b(“Quantity”), cultureInfo)
Where s=dblSum
Select New DataRow(){a,b}).toList

  • do the cartisan product with the two froms
  • filter out where a row is compaired with itself as we do want clean combinations
  • sum up
  • filter on the sum
    return a list of Datarow Array (containg in each element the pairs with the sum of 20

Output:
grafik

you will get a complet set in which also the mirrowed pairs are contained: 3,5-5,15 vs 5,15-3,5

To bring it on a next level we do need more dynamics in the LINQ as we dont want to hardcoded too much.

I hope this helped for start. Find Starter Help here:
sonnymeyer.xaml (9.4 KB)

Have a look on hows I handled the conversion 20 into double. Try to do as less as possible in LINQ when it can be done outside the do it there. This makes Bug analysis easier

5 Likes