1

我像这样在我的项目中填充了组合框

            CB_City.ItemsSource = c.Cities;
            CB_City.DisplayMemberPath = "city1";
            CB_City.SelectedValuePath = "city_id";
            CB_City.SelectedValue = 517;

我使用实体框架和 c# ,如何按升序对结果进行排序?

仍然不能(我尝试了很多)我附上了完整的代码

           using (MorEntities1  c = new MorEntities1())
        {
            CB_City.ItemsSource = c.Cities;
            CB_City.DisplayMemberPath = "city1";
            CB_City.SelectedValuePath = "city_id";
            CB_City.SelectedValue = 517;
        } 
4

3 回答 3

1

除了@Ross 的帖子,您还可以通过CollectionViewSource以下方式获得:

var view = CollectionViewSource.GetDefaultView(CB_City.ItemsSource);
view.SortDescriptions.Add(new SortDescription("city1", 
    ListSortDirection.Ascending));
于 2012-08-22T13:49:03.330 回答
1

您可以使用:CB_City.ItemsSource = c.Cities.OrderBy(c=>c.Text)CB_City.ItemsSource = c.Cities.OrderBy(c=>c.Text)

于 2012-08-22T11:40:15.303 回答
1

另一种选择是将 aCollectionViewSource与 a 一起使用SortDescription

var myViewSource = new CollectionViewSource { Source = c.Cities.ToList() };
myViewSource.SortDescriptions.Add(
  new SortDescription("YOUR_PROPERTY", ListSortDirection.Ascending)
);
CB_City.ItemsSource = myViewSource.View;
于 2012-08-22T13:41:15.167 回答