c# - select all rows that count of quantity fields least equal- Entity framework -


i have table productinventory , in have product quantity .

i want select rows least of field equals input(number) .

i try :

list<product> products = new list<product> {      new product{id=1,name="a",quantity=1},     new product{id=1,name="a",quantity=2},     new product{id=1,name="a",quantity=3},     new product{id=1,name="b",quantity=4},     new product{id=1,name="b",quantity=7} };  var result = products     .asenumerable()     .groupby(r => r.name)     .where(g => (int)g.sum(r =>r.quantity)<= 4)     .tolist(); 

but causes return zero.

example: enter image description here

i don't know possible in linq or not. can try this.

var result = products.asenumerable().where(g => g.name == "a").tolist();  int userinput =4; var total = 0; var selectlist = new list<product>(); (int = 0; < result.count; i++) {    (int j = i; j < result.count; j++)     {       if (total + result[j].quantity <= userinput)       {          total += result[j].quantity;          selectlist.add(result[j]);        }      }      if (total == userinput)        break;      else      {         total = 0;         selectlist = new list<product>();       } } if(userinput!=total)  selectlist = new list<product>(); 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -