我正在使用Dynamic Linq來執行鍼對LINQ的T-SQL where子句。除非嘗試將我手動嘗試轉換的LIKE語句與我在文章結尾處包含的函數進行轉換,否則此工作將很好。代碼甚至不是完美的,但是當我意識到在測試過程中會遇到錯誤時,我停止編程。基本上執行的代碼需要這樣的:使用數據對象的動態Linq。如何將Int32轉換爲字符串用於調用String.Contains()
「trx_no LIKE '%3500%'」
並將其轉換爲這樣的:
「trx_no.Contains(」 3500 「)」
要執行該:
昏暗X = y.Where( 「trx_no.Contains(」 3500 「)」,爲Nothing)
的錯誤: '?的Int32'
沒有適用的方法 '包含' 存在型
我認爲的問題是,我需要將可以爲空的(Int32)的「trx_no」轉換爲字符串,因此經過兩天的研究和讀書後,我想我需要將字符串中的委託函數,我無法去工作。
我自己也嘗試在這個環節here使用演員等,然而失敗與錯誤:
型表達的「布爾」預期
我的版本是這樣的:
昏暗X = y.Where( 「DirectCast(trx_no,System.String)LIKE '%35000%'」,爲Nothing)
如果我沒有包含足夠的代碼,我很抱歉,我只是不想讓這個壓倒一切。任何建議將不勝感激。謝謝。
`Private Function replaceLike(ByVal str As String) As String
Dim rtn As String = ""
If str.ToUpper.Contains(" LIKE '") Then
Dim firstQuote As Int32 = str.ToUpper.IndexOf(" LIKE '") + 6
If str.ToUpper.Chars(firstQuote + 1) = Chr(37) Then
'If the character after the first single quote is a %, this is a Contains or EndsWith
Dim secondQuote As Int32 = str.ToUpper.IndexOf("'", firstQuote + 1)
If str.ToUpper.Chars(secondQuote - 1) = Chr(37) Then
'Handles '%%', '%value%', '%'
'Found % before the last quote, this is a Contains or has the value of '%'.
Dim val = ""
'See if the value is empty so that we can extract the value
Select Case (secondQuote - 1) - (firstQuote + 1)
Case 0
'Has no value don't add
Case 1
'Has no value don't add
Case Else
val = str.Substring(firstQuote + 2, ((secondQuote - 2) - firstQuote - 1))
End Select
str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
str = str.Insert(firstQuote - 6, ".Contains(""" & val & """) ")
Else
'Handles '%value'
'Did not find another % before the last quote, this is a EndsWith
Dim val = str.Substring(firstQuote + 2, ((secondQuote - 2) - firstQuote - 1))
str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
str = str.Insert(firstQuote - 6, ".EndsWith(""" & val & """) ")
End If
Else
'Else the character after the first single quote is not a %, this is a StartWith or is Empty
Dim secondQuote As Int32 = str.ToUpper.IndexOf("'", firstQuote + 1)
If str.ToUpper.Chars(secondQuote - 1) = Chr(37) Then
'Handles 'value%'
'Found a % before the last quote, this is a StartsWith
Dim val = str.Substring(firstQuote + 2, ((secondQuote - 2) - firstQuote - 1))
str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
str = str.Insert(firstQuote - 6, ".StartsWith(""" & val & """) ")
Else
'Handles ''
'Found no %
str = str.Remove(firstQuote - 6, secondQuote - (firstQuote - 7))
str = str.Insert(firstQuote - 6, ".Contains("""") ")
End If
End If
rtn = replaceLike(str)
Else
Return str
End If
Return rtn
End Function