c# - Linq filter using List<int> -
i have database column: year (nvarchar) , want add values of column dropbox, sorted descending.
this code using:
list<string>years = db.tbl1.select(w => w.year).distinct().tolist(); years.reverse(); foreach (string year in years) { drop_years.items.add(year);}
it not working because beeing string have this: 2012, 2010, 2009, 2011, etc ..
if use:
list<int> years = db.tbl1.select(w => w.year).distinct().tolist();
the compiler tell me cannot immplicitly convert type ..< string > ..< int >...
i biginer , not know how resolve issue. please me?
i serched similar topics mine did not me figure out.
to manage problem used unprofessional method:
var years = db.tbl1.select(w => w.year).distinct(); list<int> yearslist = new list<int>(); foreach (string year in years) { yearslist.add(convert.toint32(year)); } yearslist.sort(); //i used sort() because doesn't work reverse(); yearslist.reverse();
but know not elegant solution.
thank in advance help!
all need first line changed to:
list<string>years = db.tbl1.select(w => w.year).distinct().orderby(x=>x).tolist();
the addition of order ensure content sorted when comes out database, not otherwise doing.
more neatly can use orderbydescending
, omit reverse
later.
assuming years 4 digit able string sort them fine. when have differing numbers of digits problems string sorting numbers (eg 1, 10, 2, 3). reason if numbers same number of digits each character compared of same order of magnitude (eg thousands compared thousands).
Comments
Post a Comment