2017-09-14 70 views
0

我需要存儲鍵值對列表。鍵值對將被多個類使用,所以理想情況下它的類似於我定義的結構列表。需要以下操作時存儲鍵值對的最佳數據結構

Public Structure Person 
    Public ID As Integer 
    Public Weight As Decimal 
End Structure 

'all use Person 
    class census 
    class importpeople 
    class determinebestdiet 
    etc 
  1. 我需要按值排序
  2. 我需要刪除的人(由Person.ID)或添加新鍵值對(和重新排序後各加)
  3. 我需要總計所有值的總和

似乎經常人們使用字典,然後破解。

回答

1

您可以使用List<Person>來幫助你的工作

  1. 使用List.Sort方法:
    theList.Sort(Function(x, y) x.age.CompareTo(y.age))
    使用的OrderBy擴展方法:
    theList = theList.OrderBy(Function(x) x.age).ToList()
    詳情,可看到這個帖子:https://stackoverflow.com/a/11736001/1050927

  2. 剛從列表中移除並致電SortOrderBy
    取決於你想要做什麼,你可以使用OrderBySkipTake來排序後得到特定的第n個人。

  3. 只是Sum
    PersonList.Sum(Function(per) per.Weight)

而且我相信Dictionary可以做同樣的也只是你不僅可以重用Person

相關問題