您好,我正在嘗試開發一個 Linq 查詢來查找同樣按行和列值排序的最低唯一值或最低值。
首先它執行最低唯一值完全匹配(唯一值)
如果沒有找到,那么它執行部分匹配(最低值然后最低行然后列)
具有最小值的輸出(唯一或未找到)。
資料表
| 排 | 柱子 | 價值 |
|---|---|---|
| 1 | 1 | 2 |
| 0 | 2 | 3 |
| 0 | 2 | 2 |
輸出應該是表中的最后一個資料,0,2,2因為它具有最低的行并且它是一個重復的值(第 1 行和第 0 行)。
如果添加資料2,0,0(值為 0)將否決重復的值 ( 2's)。
| 排 | 柱子 | 價值 |
|---|---|---|
| 1 | 1 | 2 |
| 0 | 2 | 3 |
| 0 | 2 | 2 |
| 2 | 0 | 0 |
Structure FoundValue
Dim Value As Short
Dim Row As Integer
Dim Column As Integer
End Structure
Dim UniqueValuesFound As New List(Of Short)
Dim ValuesFoundInPath As New List(Of FoundValue)
UniqueValuesFound.Add(0)
UniqueValuesFound.Add(2)
UniqueValuesFound.Add(3)
Dim foundValue As New FoundValue
foundValue.Value = 2
foundValue.Row = 1
foundValue.Column = 1
ValuesFoundInPath.Add(foundValue)
foundValue = New FoundValue
foundValue.Value = 3
foundValue.Row = 0
foundValue.Column = 2
ValuesFoundInPath.Add(foundValue)
foundValue = New FoundValue
foundValue.Value = 2
foundValue.Row = 0
foundValue.Column = 2
ValuesFoundInPath.Add(foundValue)
'foundValue = New FoundValue
'foundValue.Value = 0
'foundValue.Row = 2
'foundValue.Column = 0
'ValuesFoundInPath.Add(foundValue)
'New Attempt
Dim alreadyFound As Boolean = False
Dim matching = ValuesFoundInPath.Where(Function(s)
Dim index As Integer = UniqueValuesFound.BinarySearch(s.Value)
If alreadyFound = False AndAlso index >= 0 Then
alreadyFound = True
Return True 'UniqueValuesFound(index) 'exact match
ElseIf alreadyFound = False AndAlso index < 0 Then
alreadyFound = True
Return True 's.Value
Else
Return False
End If
End Function).OrderBy(Function(p) p.Value).ThenBy(Function(p) p.Row).ThenBy(Function(p) p.Column)
'New Attempt
Dim alreadyFound As Boolean = False
Dim matching = ValuesFoundInPath.Where(Function(s)
Dim index As Integer = UniqueValuesFound.BinarySearch(s.Value)
If alreadyFound = False AndAlso index >= 0 Then
alreadyFound = True
Return True 'UniqueValuesFound(index) 'exact match
ElseIf alreadyFound = False AndAlso index < 0 Then
alreadyFound = True
Return True 's.Value
Else
Return False
End If
End Function).OrderBy(Function(p) p.Value).ThenBy(Function(p) p.Row).ThenBy(Function(p) p.Column)
Look at 'New Attempt code it speaks itself, what I'm trying to do is find the first value which has a exact match in UniqueValuesFound and if it cannot find the value in UniqueValuesFound array then default to spitting out a partial match lowest value it can find. The array ValuesFoundInPath contains all the values in a random order as well as Column and Row types, which have to be sorted for partial match to find the lowest Row first.
uj5u.com熱心網友回復:
我終于解決了!,需要使用Find而不是Where問題消失了!。
答案現在需要 2 個 linq 查詢,而不是一個無法鏈接 Find
ValuesFoundInPath = ValuesFoundInPath.OrderBy(Function(p) p.Value).ThenBy(Function(p) p.Row).ThenBy(Function(p) p.Column).ToList()
Dim alreadyFound As Boolean = False
Dim matching = ValuesFoundInPath.Find(Function(s)
Dim index As Integer = UniqueValuesFound.BinarySearch(s.Value)
If alreadyFound = False AndAlso index >= 0 Then
alreadyFound = True
Return True 'UniqueValuesFound(index) 'exact match
ElseIf alreadyFound = False AndAlso index < 0 Then
alreadyFound = True
Return True 's.Value
Else
Return False
End If
End Function)
Dim result As New Result
result.Answer = matching.Value
result.CurrentRow = matching.Row
result.CurrentColumn = matching.Column
您可能會遇到問題,因為Find(...)總是對值串列進行預排序。所以你需要使用FindIndex(...)它來避免排序問題。
Dim matchingIndex = ValuesFoundInPath.FindIndex(Function(s)
Dim index As Integer = UniqueValuesFound.BinarySearch(s.Value)
If alreadyFound = False AndAlso index >= 0 Then
alreadyFound = True
Return True 'UniqueValuesFound(index) 'exact match
ElseIf alreadyFound = False AndAlso index < 0 Then
alreadyFound = True
Return True 's.Value
End If
Return False 'No match.
End Function)
If matchingIndex < 0 Then MessageBox.Show("Wtf!")
Dim result As New Result
result.Answer = ValuesFoundInPath(matchingIndex).Value
result.CurrentRow = ValuesFoundInPath(matchingIndex).Row
result.CurrentColumn = ValuesFoundInPath(matchingIndex).Column
它使OrderBy(...)無用,因為Find(...)通過遞增值對串列進行預排序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/335612.html
上一篇:隨機化影像將進入的圖片框
下一篇:如何洗掉文本檔案中的多個空行
