Can you sort Typed DataSet DataTables with Linq OrderBy?

后端 未结 2 1589
梦毁少年i
梦毁少年i 2021-01-25 22:05

I have a Typed DataSet DataTable which inherits TypedTableBase, which in turn implements IEnumerable. I can\'t seem to get this to wo

2条回答
  •  一个人的身影
    2021-01-25 22:51

    Linq extension methods do not alter the source enumerable.

    var numbers = new int[]{1,2,3};
    var reversed = numbers.OrderByDescending(x=>x);
    foreach(var number in reversed)
      Console.Write(number); // 321
    foreach(var number in numbers)
      Console.Write(number); // 123
    

    If you want to sort a DataTable, you should be using DataViews. You create a view on your DataTable, then apply a Sort or Filter to it, then bind against it. Keep in mind DataSets are an older technology and not quite up to date on the latest and the greatest. A "newer" approach would be to use the Entity Framework.

提交回复
热议问题