排序 UWP DataGrid 时改进 Linq 查询

Improve Linq query when sorting UWP DataGrid

我按照 How to: Group, sort and filter data in the DataGrid Control

中的示例在 UWP DataGrid 中实现了排序

列排序事件使用来自数据列header的Tag到运行特定的Linq查询,例如Tag=“Country”。

我假设的数据网格有 3 个字段“Country”、“Name”、“Height”,所以为了向所有 3 个字段添加排序,我需要复制逻辑并仅使用“ orderby 字段”是不同的。有更好的解决方案吗?

if (e.Column.Tag.ToString() == "Country") 
{
  mydg.ItemsSource = new ObservableCollection<Mountain>(
  from item in myData
  orderby item.Country ascending 
  select item);
 }

看看Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

您可以构建基本 IQueryable<>,根据用户选择的排序首选项动态应用排序,然后将生成的查询作为 ItemSource.

传递