使用数据对象的动态Linq。如何将Int32转换为String以调用String.Contains()
我使用动态Linq对Linq执行一个T-SQL where子句。除了试图转换LIKE语句外,这非常有效,我已经手动尝试使用本文末尾包含的函数来转换LIKE语句。这段代码甚至还不够完美,但当我意识到在测试过程中会出现错误时,我停止了编程。执行的代码基本上是这样的:使用数据对象的动态Linq。如何将Int32转换为String以调用String.Contains(),linq,linq-to-objects,vb.net-2010,Linq,Linq To Objects,Vb.net 2010,我使用动态Linq对Linq执行一个T-SQL where子句。除了试图转换LIKE语句外,这非常有效,我已经手动尝试使用本文末尾包含的函数来转换LIKE语句。这段代码甚至还不够完美,但当我意识到在测试过程中会出现错误时,我停止了编程。执行的代码基本上是这样的: Select Case FindMethod(type, id, instance Is Nothing, args, mb) Case 0
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
“trx\u没有像'%3500%'”
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
并将其转换为:
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
“trx\u编号包含(“3500”)”
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
要执行此操作:
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
尺寸x=y,其中(“trx\U编号包含(“3500”),无任何内容)
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
错误:
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
类型“Int32”中不存在适用的方法“Contains”
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
我认为问题是,我需要将“trx_no”(Int32的一个可空值)转换为字符串,因此经过两天的研究和阅读,我认为我需要将该字符串转换为委托函数,而我无法使用该函数
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
我也尝试在这个链接中使用Cast-like,但是失败了,出现了一个错误:
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
应为“布尔”类型的表达式
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
我的版本如下:
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
Dim x=y.Where(“DirectCast(trx\u no,System.String)如“%35000%”,无任何内容)
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
如果我没有包括足够的代码,我很抱歉,我只是不想让这成为压倒性的。如有任何建议,将不胜感激。多谢各位
`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
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
我发现动态Linq库中预定义的类型支持Convert,我使用Convert来表示以下内容:
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
“Convert.ToString(“&propertyName&”)包含(“&val&”)
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
如果上面的propertyName是可空类型,则这会导致将可空(Of)转换为字符串的问题。在ParseMemberAccess方法的动态Linq库中编辑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