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.
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
Post a Comment