开发者

How sort data taking from database in C#

There is state table that have statename and circode but they are not sorted in table. I use OrderByAsc("StateName") where StateName is a column name of state table. How I get sorted state in dropdownlist. I use subsonic to fetch da开发者_开发百科ta from database.

StateCollection stc = (new 
StateController()).getInstance().WFetchAll(true);


    ddlState.Items.Clear();
    if(addAllStates)
        ddlState.Items.Add(new ListItem("All States", "0"));

    //ddlState.DataSource = State.FetchAll();
    //ddlState.DataTextField = State.Columns.StateName;
    //ddlState.DataValueField = State.Columns.CircleCode;
    //ddlState.DataBind();
    stc = stc.OrderByAsc("CircleCode");
    stc.SaveAll();
    foreach (State st in stc)
    {
        ddlState.Items.Add(new ListItem(st.CircleCode + "-" + st.StateName, st.CircleCode));
    }


Try

foreach (State st in stc.OrderBy(s => s.StateName))
{
    ddlState.Items.Add(new ListItem(st.CircleCode + "-" + st.StateName, st.CircleCode));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜