如何从C#中的数据库中获取数据排序

时间:2011-07-06 07:14:12

标签: c# asp.net database

有状态表有statename和circode但它们没有在表中排序。 我使用OrderByAsc("StateName"),其中StateName是状态表的列名。我如何在下拉列表中获得排序状态。我使用subsonic从数据库中获取数据。

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));
    }

1 个答案:

答案 0 :(得分:2)

尝试

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