|
4 | 4 | // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
5 | 5 |
|
6 | 6 | using System.ComponentModel.DataAnnotations; |
| 7 | +using System.Data; |
7 | 8 | using System.Dynamic; |
8 | 9 | using System.Linq.Expressions; |
9 | 10 |
|
@@ -416,6 +417,62 @@ public void Sort_Queryable_Complex() |
416 | 417 | Assert.Equal(10, orderFoos.ElementAt(0).Foo!.Count); |
417 | 418 | } |
418 | 419 |
|
| 420 | + [Fact] |
| 421 | + public void Sort_IDynamicObject_Ok() |
| 422 | + { |
| 423 | + var dataTable = new DataTable(); |
| 424 | + |
| 425 | + DataColumn column = new DataColumn |
| 426 | + { |
| 427 | + DataType = Type.GetType("System.Int32"), |
| 428 | + ColumnName = "ID" |
| 429 | + }; |
| 430 | + dataTable.Columns.Add(column); |
| 431 | + |
| 432 | + column = new DataColumn |
| 433 | + { |
| 434 | + DataType = Type.GetType("System.String"), |
| 435 | + ColumnName = "Name" |
| 436 | + }; |
| 437 | + dataTable.Columns.Add(column); |
| 438 | + |
| 439 | + //Creating some rows |
| 440 | + DataRow row = dataTable.NewRow(); |
| 441 | + row["ID"] = 1; |
| 442 | + row["Name"] = "Bob"; |
| 443 | + dataTable.Rows.Add(row); |
| 444 | + |
| 445 | + row = dataTable.NewRow(); |
| 446 | + row["ID"] = 3; |
| 447 | + row["Name"] = "Adam"; |
| 448 | + dataTable.Rows.Add(row); |
| 449 | + |
| 450 | + row = dataTable.NewRow(); |
| 451 | + row["ID"] = 2; |
| 452 | + row["Name"] = "Jane"; |
| 453 | + dataTable.Rows.Add(row); |
| 454 | + |
| 455 | + var context = new DataTableDynamicContext(dataTable, (context, col) => { }); |
| 456 | + var items = context.GetItems().ToList(); |
| 457 | + |
| 458 | + // 未排序 |
| 459 | + Assert.Equal("Bob", items[0].GetValue("Name")); |
| 460 | + Assert.Equal("Adam", items[1].GetValue("Name")); |
| 461 | + Assert.Equal("Jane", items[2].GetValue("Name")); |
| 462 | + |
| 463 | + // Name 排序 |
| 464 | + var nameItems = items.Sort("Name", SortOrder.Asc).ToList(); |
| 465 | + Assert.Equal("Adam", nameItems[0].GetValue("Name")); |
| 466 | + Assert.Equal("Bob", nameItems[1].GetValue("Name")); |
| 467 | + Assert.Equal("Jane", nameItems[2].GetValue("Name")); |
| 468 | + |
| 469 | + // Name 倒序 |
| 470 | + nameItems = items.Sort("Name", SortOrder.Desc).ToList(); |
| 471 | + Assert.Equal("Adam", nameItems[2].GetValue("Name")); |
| 472 | + Assert.Equal("Bob", nameItems[1].GetValue("Name")); |
| 473 | + Assert.Equal("Jane", nameItems[0].GetValue("Name")); |
| 474 | + } |
| 475 | + |
419 | 476 | [Fact] |
420 | 477 | public void GetPropertyValueLambda_Null() |
421 | 478 | { |
|
0 commit comments