我有这个问题
Structure Person
Dim name As String
Dim age As Integer
End Structure
Dim people(10) As Person
people(0).name = "A"
people(0).age = 21
...
people(9).name = "Z"
people(9).age = 30
现在,例如如果我有age = {20, 21, 20, 23, 25, 35, 30, 29, 25, 26}
我正在尝试识别并消除具有独特年龄的people
中的每个人,即我的最终people
数组应仅包含年龄为20,25
的人的记录。
我试过这样的事情:
tempList() As Int
cleanList() As Int
for i = 0 to people.count - 1
array.resize(templist, i + 1)
templist(i) = people(i).age
next
cleanList = tempList.Distinct().ToArray()
问题是,这样做可以从数组中获得每个不同的值。但我只想要那些只出现一次的值。
对此有何帮助?
非常感谢!
答案 0 :(得分:1)
您可以按.age
进行分组,并从包含多个项目的组中获取项目:
Dim cleanList = people.ToLookup(Function(p) p.age).
Where(Function(g) g.Count > 1).SelectMany(Function(g) g).ToList
答案 1 :(得分:0)
您需要先对年龄列表进行分组才能获得重复的值:
Dim ages= From a In allages _
Group a By Key = a Into Group _
Where Group.Count() > 1 _
Select Group.Key
然后你可以过滤你的人名单:
Dim result = From p In people _
Where ages.Contains(p.age) _
Select p
答案 2 :(得分:0)
试试这个:
Dim duplicatePersons = people.Where(Function(p) people.Count(Function(p1) p1.Age = p.Age) > 1).ToArray()