enter image description here 如何通過LINQ獲取表中用戶的排名?我用VB.NET代碼查找LINQ的用戶排名
,如果我們有我們的表這些領域
[ID,姓名,成績]
爲了用戶,我們怎麼能拿到名次(行號)的分數後一個他們在我們的查詢?
enter image description here 如何通過LINQ獲取表中用戶的排名?我用VB.NET代碼查找LINQ的用戶排名
,如果我們有我們的表這些領域
[ID,姓名,成績]
爲了用戶,我們怎麼能拿到名次(行號)的分數後一個他們在我們的查詢?
謝謝你的男人。我用波紋管的代碼,它的工作對我罰款:
Dim query = (From P In DB.tblUserScores Order By P.Score
Descending Select P).AsEnumerable()
Dim Rank As Integer = 1
For Each userObj As tblUserScore In query
If userObj.DeviceId = DeviceID Then Exit For
Rank += 1
Next
是的,這是更好的方法,但我認爲它應該返回Rank = 5(因爲A1和A7都有7分數)我們該如何解決這個問題?謝謝 – Ali
方法如下:
Dim records = _
{ _
New With { Key .ID = 1, Key .Name = "A1", Key .Score = 7 }, _
New With { Key .ID = 2, Key .Name = "A2", Key .Score = 9 }, _
New With { Key .ID = 3, Key .Name = "A3", Key .Score = 2 }, _
New With { Key .ID = 4, Key .Name = "A4", Key .Score = 1 }, _
New With { Key .ID = 5, Key .Name = "A5", Key .Score = 6 }, _
New With { Key .ID = 6, Key .Name = "A6", Key .Score = 4 }, _
New With { Key .ID = 7, Key .Name = "A7", Key .Score = 7 }, _
New With { Key .ID = 8, Key .Name = "A8", Key .Score = 3 }, _
New With { Key .ID = 9, Key .Name = "A9", Key .Score = 5 }, _
New With { Key .ID = 10, Key .Name = "A10", Key .Score = 8 } _
}
Dim query = _
From r In Records _
Order By r.Name Ascending
Order By r.Score Descending
Select r
query.Dump()
Dim rank = _
query _
.Select(Function (x, n) New With { Key .Record = x, Key .Rank = n + 1 }) _
.ToDictionary(Function (x) x.Record.Name, Function (x) x.Rank)
Dim name = "A9"
Console.WriteLine("User " & name & "'s Rank is " & rank(name))
這給了我:
User A9's Rank is 6
凌?聽起來太酷了。我想得到一盒 – Plutonix
你不會爲此使用LINQ。 – jmcilhinney
當你有兩個記錄相同的分數,你如何確定分數是什麼?在你的例子中,你有''A1''''A7「'具有相同得分'7',但''A1''具有等級'3'和''A7」'具有等級4。系統他們都分享'3',並且排名會直接跳到下一個'5'。你想怎麼做? – Enigmativity