Home > OS >  Get rows according to given value
Get rows according to given value

Time:07-07

I have mssql table as the given image. I want to know how can I return rows from the table tblSpace that the sum of "Available" rows is greater than or equal to given value using Linq. Also there is a order of filling from 1 to 5. Thanks

Table capture and data:> tblSpace

What I've tried so far

1). If I pass 9,000 as value it should return only Vienguard row (9,000 < 10,000)

2). If I pass 23,000 as value it should return both Vienguard and Halestorm rows (23,000 < 10,000 15,000)

3). if I pass 35,000 as value it should return Vienguard, Halestorm and Quarts rows (35,000 < 10,000 15,000 20,000)

CodePudding user response:

Alright so I think you need something like this?

var sum = 0;
var result = tblSpace.OrderBy(x => x.Available).TakeWhile(x => (sum  = x.Available) <= value).ToList();

EDIT In case you want an extra value if it exceeds you add

var result = tblSpace.OrderBy(x => x.Available).TakeWhile(x => (sum  = x.Available) - x.Available < value).ToList();
  • Related