asp.net - Get minimun value from list -
i have class abc this
public class abc{ public int id {get;set;} public int usercount {get;set;} } now add following records list of type abc
list<abc> lstabc = new list<abc>(); lstabc.add(new abc(){id=1,usercount=5}); lstabc.add(new abc(){id=2,usercount=15}); lstabc.add(new abc(){id=3,usercount=3}); lstabc.add(new abc(){id=4,usercount=20}); i've list of type int
list<int> lstids = new list<int>(); lstids.add(1); lstids.add(3); lstids.add(4); now want find out minimum value of usercount comparing both list id in lstabc should match items presesnt in lstids. can minimum value of usercount using loops there way value in optimized way avoiding loops?
you can use enumerable.join link both lists:
var joined = id in lstids join x in lstabc on id equals x.id select x; int minvalue = joined.min(x => x.usercount); this more efficient loops since join uses set find matching items.
there's more 1 way skin cat, little bit less efficient:
int minvalue = lstabc.where(x => lstids.contains(x.id)).min(x => x.usercount);
Comments
Post a Comment