c# - .OrderBy in Linq query not always ordering properly -
below linq query i'm using in asp mvc 3 controller try , order results alphabetically state code. 99% of time works fine, i'm finding couple lists unordered so
for each bank list in index, need pull secondary table banklistagentid
, display agents have working particular bank. majority of time agents ordered according state code properly, few times wind unordered in screen shot above.
foreach (var bank in banklist) { bank.banklistagentid = (from in db.banklistagentid a.bankid == bank.id select a).orderby(x => x.statecode, stringcomparer.currentcultureignorecase).tolist(); }
edit
i tried separating out operations individual lines so, got same results (99% ordered properly, 1% not)
foreach (var bank in banklist) { var agent = (from in db.banklistagentid a.bankid == bank.id select a).tolist(); agent = agent.orderby(x => x.statecode).tolist(); bank.banklistagentid = agent.tolist(); }
since orderby() on inside of foreach(var bank in banklist) loop, affect each individual query runs in each iteration. need orderby clause occur after queries have finished.
try instead:
banklist.selectmany(bank => bank.banklistagentid).orderby(a => a.statecode, stringcomparer.currentcultureignorecase).tolist();
Comments
Post a Comment