Vb.net 创建不能重复的名称列表(如果重复,请在名称中添加编号)
我有一个类Person的objectList,它有一个Name属性。我的目标是最终得到一个唯一的名字列表。任何副本都必须在末尾添加一个数字 例如,假设我们正在循环的对象具有如下名称:Vb.net 创建不能重复的名称列表(如果重复,请在名称中添加编号),vb.net,linq,Vb.net,Linq,我有一个类Person的objectList,它有一个Name属性。我的目标是最终得到一个唯一的名字列表。任何副本都必须在末尾添加一个数字 例如,假设我们正在循环的对象具有如下名称: 约翰,迈克,皮特,马克,皮特,亚当,斯坦,皮特 我的唯一列表应该是这样的: 约翰,迈克,皮特,马克,皮特02,亚当,斯坦,皮特03 我正在尝试执行以下操作,但在Pete第三次进入时无法执行: Dim nameList As List(Of String) Dim uniqueList As List(Of Stri
约翰,迈克,皮特,马克,皮特,亚当,斯坦,皮特
我的唯一列表应该是这样的:
约翰,迈克,皮特,马克,皮特02,亚当,斯坦,皮特03 我正在尝试执行以下操作,但在Pete第三次进入时无法执行:
Dim nameList As List(Of String)
Dim uniqueList As List(Of String)
For Each object In objectList
Dim name = object.Name
If nameList.Any(Function(str) str = name) Then
name = name + "02"
End If
nameList.Add(object.Name)
uniqueList.Add(name)
Next
试着替换
name = name + "02"
与
后者统计唯一列表中Pete*(=Pete、Pete02等)的出现次数,因此知道要追加的数字。注意:如果列表中有例如“Pam”和“Pamela”,则下一个Pam将得到第3个Pam,因为“Pamela”也以“Pam”开头。尝试替换
name = name + "02"
与
后者统计唯一列表中Pete*(=Pete、Pete02等)的出现次数,因此知道要追加的数字。注意:如果列表中有例如“Pam”和“Pamela”,则下一个Pam将获得第3名,因为“Pamela”也以“Pam”开头。与LINQ:
Dim namesList = New String() {"Pete", "Mary", "Pete", "Pete"}
Dim namesListUnique = namesList.Select(Function(item, index) New With { _
.OriginalName = item, _
.Index = index _
}).Select(
Function(item)
Dim nbBefore = namesList.Take(item.Index).Count(Function(i) i = item.OriginalName)
Dim uniqueName = item.OriginalName
If nbBefore > 0 Then
uniqueName &= (nbBefore + 1).ToString("00")
End If
Return uniqueName
End Function _
).ToList()
林克:
Dim namesList = New String() {"Pete", "Mary", "Pete", "Pete"}
Dim namesListUnique = namesList.Select(Function(item, index) New With { _
.OriginalName = item, _
.Index = index _
}).Select(
Function(item)
Dim nbBefore = namesList.Take(item.Index).Count(Function(i) i = item.OriginalName)
Dim uniqueName = item.OriginalName
If nbBefore > 0 Then
uniqueName &= (nbBefore + 1).ToString("00")
End If
Return uniqueName
End Function _
).ToList()
试试这个:-
Dim result = names.GroupBy(Function(x) x) _
.SelectMany(Function(x) x.[Select](Function(v, i) New With { _
Key .Value = v, Key .Index = i}) _
.Select(Function(z) z.Value + (If(z.Index <> 0,
(z.Index + 1).ToString(), [String].Empty))))
试试这个:-
Dim result = names.GroupBy(Function(x) x) _
.SelectMany(Function(x) x.[Select](Function(v, i) New With { _
Key .Value = v, Key .Index = i}) _
.Select(Function(z) z.Value + (If(z.Index <> 0,
(z.Index + 1).ToString(), [String].Empty))))
我会用字典来储存号码
Dim namesList = New String() {"John", "Mike", "Pete", "Mark", "Pete", "Adam", "Stan", "Pete"}
Dim uniqueList As New Dictionary(Of String, Integer)
For Each name As String In namesList
Dim uniqueName As String
uniqueName = name
If Not uniqueList.ContainsKey(name) Then
uniqueList.Add(name, 1)
Else
uniqueList(name) += 1
uniqueName &= uniqueList(name).ToString("00")
End If
Console.WriteLine(uniqueName)
Next
我会用字典来储存号码
Dim namesList = New String() {"John", "Mike", "Pete", "Mark", "Pete", "Adam", "Stan", "Pete"}
Dim uniqueList As New Dictionary(Of String, Integer)
For Each name As String In namesList
Dim uniqueName As String
uniqueName = name
If Not uniqueList.ContainsKey(name) Then
uniqueList.Add(name, 1)
Else
uniqueList(name) += 1
uniqueName &= uniqueList(name).ToString("00")
End If
Console.WriteLine(uniqueName)
Next
什么是不工作?第三次它做了什么?你有机会看看我的答案吗?什么不起作用?第三次它做了什么?你有机会看我的答案吗?我在说uniqueList时出错。Count不带参数。你引用了
System.Linq
?我在说uniqueList时出错。Count不带参数。你引用了System.Linq
?