如何在C#中对数据库中的数据进行排序
本文关键字:数据 排序 数据库 | 更新日期: 2023-09-27 18:01:11
有一个状态表有statename和circode,但它们没有在表中排序。我使用OrderByAsc("StateName")
,其中StateName是状态表的列名。如何在下拉列表中获得排序状态。我使用亚音速从数据库中提取数据。
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));
}
尝试
foreach (State st in stc.OrderBy(s => s.StateName))
{
ddlState.Items.Add(new ListItem(st.CircleCode + "-" + st.StateName, st.CircleCode));
}