使用数据对象的动态 Linq。如何将 Int32 转换为 String 以调用 String.Contains()
我正在使用 Dynamic Linq 针对 LINQ 执行 T-SQL where 子句。这非常有效,除非尝试转换 LIKE 语句(我已手动尝试使用我在帖子末尾包含的函数进行转换)。该代码还远未接近完美,但当我意识到在测试过程中会出现错误时,我停止了编程。执行的代码本质上采用:
“trx_no like '%3500%'”
并将其转换为:
“trx_no.Contains(“3500”)”
要执行此:
Dim x = y.Where("trx_no.Contains("3500")", Nothing)
错误:
没有适用的方法'Contains'存在于'Int32类型中?'
我认为问题是我需要将Nullable(Of Int32)的“trx_no”转换为字符串,所以经过两天的研究和书籍阅读我认为我需要在委托函数中转换字符串,但我无法开始工作。
我还尝试像此链接中那样使用 Cast 此处,但是失败并出现错误:
“Boolean”类型的表达式预期
我的版本如下所示:
Dim 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
I am using Dynamic Linq to execute a T-SQL where clause against LINQ. This works great except when trying to convert the LIKE statement which I have manually attempted to convert with a function that I have included at the end of the post. The code is not even close to being perfect, but I stopped programming this when I realized that I would get an error during testing. The code that executes essentially takes this:
"trx_no like '%3500%'"
And converts it to this:
"trx_no.Contains("3500")"
To execute this:
Dim x = y.Where("trx_no.Contains("3500")", Nothing)
The Error:
No applicable method 'Contains' exists in type 'Int32?'
The issue I think is that I need to convert the "trx_no" which is a Nullable(Of Int32) to a String, so after two days of research and book reading I figured that I would need to convert the string in a delegate function which I can not get to work.
I have also tried to use Cast like in this link here, however this fails with an error:
Expression of type 'Boolean' expected
My version looked like this:
Dim x = y.Where("DirectCast(trx_no, System.String) like '%35000%'", Nothing)
If I have not included enough code I am sorry I just did not want to make this to overwhelming. Any suggestions would be much appreciated. Thank You.
`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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现动态 Linq 库中的预定义类型支持 Convert,我用它来制定以下内容:
"Convert.ToString(" & propertyName & ").Contains(""" & val & "" 如果上面的 propertyName 是 Nullable 类型,
这会导致将 Nullable(Of ) 转换为字符串时出现问题。在动态 Linq 库中编辑 ParseMemberAccess 方法的 Dynamic.vb 代码可以使转换正常进行。以下是对该方法中的 Select Case 语句所做的编辑:
I found that the predefinedTypes in the Dynamic Linq Library supported Convert, which I used to formulate the following:
"Convert.ToString(" & propertyName & ").Contains(""" & val & """)"
This lead to an issue with converting Nullable(Of ) to a string if the propertyName above was a Nullable type. Editing the Dynamic.vb code in the Dynamic Linq Library for the ParseMemberAccess method allowed the conversion to work. Below is the edit made to the Select Case statement in that method: