asked on
Dim stringarray1 As String()
stringarray1 = {"hello","bye","bye"}
Dim stringarray2 As String()
stringarray2 = {"hello","hello","bye"}
Dim result1 As String()
result1 = stringarray1.Except(stringarray2)
Console.WriteLine(string.join(vbNewLine,result1))
' outputs nothing, but the desired output is "bye"
Dim result2 As String()
result2 = stringarray2.Except(stringarray1)
Console.WriteLine(string.join(vbNewLine,result2))
' outputs nothing, but the desired output is "hello"
result1 = From s1 In stringarray1
Group Join s2 In stringarray2
On s1 Equals s2 Into Any
Where Not Any
Select s1
ASKER
Module ModuleExtensions
<Extension()>
Public Function ExceptWithDuplicates(aStringArray1 As String(),aStringArray2 As String()) As String()
Dim lStringArray = aStringArray1 _
.GroupBy(Function(x) x) _
.SelectMany(Function(x) _
x.Where(Function(y,i) _
i>0 OrElse Not aStringArray2.Contains(y))) _
.ToArray
Return lStringArray
End Function
End Module
Dim stringarray1 As String()
stringarray1 = {"hello","hello","bye","bye"}
Dim stringarray2 As String()
stringarray2 = {"hello","hello","hello","bye","bye"}
results: "hello"
results: "hello", "hello", "bye"
result1 = stringarray1.GroupJoin(stringarray2,
Function(s1) s1,
Function(s2) s2,
Function(s1, sa2) New With {s1, sa2}).
Where(Function(x) Not x.sa2.Any).
Select(Function(x) x.s1)
Linq syntax is clearer.
ASKER
Dim result1 = stringarray1.
GroupBy(Function(s) s).
GroupJoin(stringarray2,
Function(s) s.Key,
Function(s) s,
Function(s1, s2) New With {
s1.Key,
.Count = s1.Count - s2.Count
}).
Where(Function(x) x.Count > 0).
SelectMany(Function(x) Enumerable.Repeat(x.Key, x.Count))
Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,
TRUSTED BY
You could use an extension method like the following:
Open in new window
Essentially i am grouping the first array and figure out the count of each string. Then i am getting only those that either have a count of more than one or the ones that do not exist on the second array.
You may use this as in the example below:
Open in new window