简体   繁体   English

vb.net中的头寸订单

[英]Position Order in vb.net

我正在做一个项目,得分最高的人将担任职位1,下一个人将成为职位2。例如,如果两个人的得分相同,他们将给出相同的职位,但接下来的人则是其他人,例如两个得分都为65将具有相同的位置即1,而下一个内联将不是2而是3。

This can be achieved via LINQ: 这可以通过LINQ实现:

    ' Example user scores
    Dim userScores = {
        New With {.Score = 65, .Name = "Linda"},
        New With {.Score = 65, .Name = "Paul"},
        New With {.Score = 64, .Name = "John"},
        New With {.Score = 63, .Name = "Yoko"}
    }
    ' Transform user scores to a high score table
    Dim table = _
        userScores _
        .OrderByDescending(Function(user) user.Score) _
        .Select(Function(user, i) New With {.Index = i + 1, .User = user}) _
        .GroupBy(Function(row) row.User.Score) _
        .SelectMany(
            Function(group)
                Dim position = group.First().Index
                Return group.Select(
                    Function(row)
                    Return New With {.Position = position, .User = row.User}
                End Function)
            End Function)
    ' Print results
    For Each row In table
        Console.WriteLine("{0} {1} {2}", row.Position, row.User.Score, row.User.Name)
    Next

Here the users are ordered (descending) by score. 在此,用户按分数排序(降序)。 Then an index is given to each user. 然后给每个用户一个索引。 The users are then grouped by score. 然后将用户按分数分组。 Finally the groups are flattened using SelectMany, with the position for each row being assigned from the the first row in the group's index value. 最后,使用SelectMany将组扁平化,并从组索引值的第一行开始分配每一行的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM