Public Class ListComparison
Public Function CompareTwoLists(Of T1, T2, TDuplicate As DuplicateExpression)(list1 As IEnumerable(Of T1), list2 As IEnumerable(Of T2), compareValue As Func(Of T1, T2, Boolean), DuplicateExpression As List(Of TDuplicate)) As Boolean
Return list1.[Select](Function(item1) list2.Any(Function(item2) compareValue(item1, item2))).All(Function(search) search) AndAlso list2.[Select](Function(item2) list1.Any(Function(item1) compareValue(item1, item2))).All(Function(search) search)
End Function
Public Function CompareLists(Of T1, TDuplicate As DuplicateExpression)(list1 As IEnumerable(Of T1), list2 As IEnumerable(Of T1), DuplicateExpression As List(Of TDuplicate)) As String
Dim InvalidDuplicateExpression As List(Of TDuplicate) = New ExtensionHelper().ValidateColumnInList(Of T1, TDuplicate)(DuplicateExpression)
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
If InvalidDuplicateExpression.Count() <= 0 Then
Dim Fields As String() = DuplicateExpression.[Select](Function(x) x.ExpressionName).ToArray()
Dim JoinExp = list1.Join(list2, Fields)
Dim IsDuplicate As Boolean = True
IsDuplicate = CompareTwoLists(list1, list2, Function(listx1, listx2) JoinExp.Any(), DuplicateExpression)
If IsDuplicate Then
Return js.Serialize(New With { _
Key .IsValidateExpression = True, _
Key .IsValidateDuplicate = IsDuplicate, _
Key .Message = "Duplicate Items in the List" _
})
Else
Return js.Serialize(New With { _
Key .IsValidateExpression = True, _
Key .IsValidateDuplicate = IsDuplicate _
})
End If
Else
Return js.Serialize(New With { _
Key .IsValidateExpression = False, _
Key .InvalidExpressions = InvalidDuplicateExpression, _
Key .Message = "Duplicate Expression is not Invalid" _
})
End If
End Function
Public Shared Function CreateSelector(Of T)(PropertyNames As IEnumerable(Of String)) As Func(Of T, Object)
Dim SourceType = GetType(T)
Dim Parameter = Expression.Parameter(SourceType, "e")
Dim Properties = PropertyNames.[Select](Function(Name) Expression.PropertyOrField(Parameter, Name)).ToArray()
Dim Selector = Expression.Lambda(Of Func(Of T, Object))(Expression.[Call](GetType(Tuple), "Create", Properties.[Select](Function(p) p.Type).ToArray(), Properties), Parameter)
Return Selector.Compile()
End Function
Public Shared Function Join(Of T)(Left As IEnumerable(Of T), Right As IEnumerable(Of T), PropertyNames As IEnumerable(Of String)) As IEnumerable(Of Tuple(Of T, T))
Dim KeySelector = CreateSelector(Of T)(PropertyNames)
Return Left.Join(Right, KeySelector, KeySelector, AddressOf Tuple.Create)
End Function
End Class
当我编译这段代码时,我得到以下错误:
错误15重载解析失败,因为没有可访问的"Join"接受此数量的参数。
Dim JoinExp = list1.Join(list2, Fields)
错误在上面一行。这主要用于列表与动态列属性的比较。这段代码可以在c# .net中正常工作。当我将c# .net代码转换为Vb.net时;得到这个错误。如何在Vb.net中解决这个问题?
显然c# Join
函数是一个扩展方法
public static IEnumerable<Tuple<T, T>> Join<T> (
this IEnumerable<T> Left,
IEnumerable<T> Right,
IEnumerable<string> PropertyNames
)
在这种情况下,您必须在VB.NET中添加<Extension>
属性。