我正在使用Dynamic Linq来执行针对LINQ的T-SQL where子句。除了尝试转换我手动尝试使用我在帖子末尾包含的函数转换的LIKE语句时,这很有用。代码甚至不是很完美,但当我意识到在测试过程中会出现错误时,我就停止编程了。执行的代码基本上是这样的:
“trx_no喜欢'%3500%'”
并将其转换为:
“trx_no.Contains(” 3500 “)”
执行此操作:
Dim x = y.Where(“trx_no.Contains(”3500“)”,Nothing)
错误:
“Int32”类型中是否存在适用的方法“包含”?
我认为这个问题是我需要将一个Nullable(Of Int32)的“trx_no”转换为String,所以在经过两天的研究和书籍阅读之后,我想我需要将字符串转换成一个字符串。代表我无法工作的功能。
我也尝试在此链接here中使用Cast,但是这会失败并显示错误:
预期类型'Boolean'的表达式
我的版本看起来像这样:
Dim x = y.Where(“DirectCast(trx_no,System.String)”如'%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
答案 0 :(得分:2)
我发现Dynamic Linq Library中的predefinedTypes支持转换,我用它来制定以下内容:
"Convert.ToString(" & propertyName & ").Contains(""" & val & """)"
如果上面的propertyName是Nullable类型,则会导致将Nullable(Of)转换为字符串的问题。在Dynamic Linq Library中为ParseMemberAccess方法编辑Dynamic.vb代码允许转换工作。下面是对该方法中的Select Case语句所做的编辑:
Select Case FindMethod(type, id, instance Is Nothing, args, mb)
Case 0
Throw ParseError(errorPos, Res.NoApplicableMethod, id, GetTypeName(type))
Case 1
Dim method = DirectCast(mb, MethodInfo)
If (Not IsPredefinedType(method.DeclaringType)) Then
Throw ParseError(errorPos, Res.MethodsAreInaccessible, GetTypeName(method.DeclaringType))
End If
If method.ReturnType.Equals(GetType(Void)) Then
Throw ParseError(errorPos, Res.MethodIsVoid, id, GetTypeName(method.DeclaringType))
End If
Dim newargs As Expression() = args
For Each a As Expression In args
If a.Type.IsGenericType AndAlso a.Type.GetGenericTypeDefinition = GetType(Nullable(Of )) Then
newargs(Array.IndexOf(args, a)) = System.Linq.Expressions.Expression.Convert(a, GetType(Object))
Else
newargs(Array.IndexOf(args, a)) = a
End If
Next
Return Expression.Call(instance, DirectCast(method, MethodInfo), newargs)
Case Else
Throw ParseError(errorPos, Res.AmbiguousMethodInvocation, id, GetTypeName(type))
End Select