How to append unique numbers to a list of strings

后端 未结 2 1303
北恋
北恋 2021-01-25 08:02

I have this function, which works and gives the correct result:

  
  Pu         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-25 08:26

    Here's the VB version:

      
      Public Function Unique(List As List(Of String)) As List(Of String)
        ' 1. Remember the initial order
        ' 2. Group by the text
        ' 3. Label the entries in each group
        ' 4. Now reorder them by their original order
        ' 5. Remove the order value to get back to just the name
        Return List.
          Select(Function(Item, Index) New With {Item, Index}).
          GroupBy(Function(IndexedItem) IndexedItem.Item).
          SelectMany(Function(Group) Group.Select(Function(GroupItem, GroupIndex) New With {.Index = GroupItem.Index, .UniqueItem = GroupItem.Item & If(GroupIndex = 0, String.Empty, (GroupIndex + 1).ToString)})).
          OrderBy(Function(IndexedUniqueItem) IndexedUniqueItem.Index).
          Select(Function(IndexedUniqueItem) IndexedUniqueItem.UniqueItem).
          ToList()
      End Function
    

提交回复
热议问题