Click here to Skip to main content
16,005,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given two arrays A and B, find the index from A of the element of B

Dim A As String() = {"a", "b", "c", "d", "e", "f", "g", "h", "i"}
Dim B As String() = {"b", "d", "e"}

For Each item As String In B
    Dim index1 As Integer = Array.IndexOf(A, item)
    Console.WriteLine("{0} {1}", index1, item)
Next


1 b
3 d
4 e

What I have tried:

My code in 'traditional' VB.NET works, but I want to convert it using LINQ.
Thanks in advance.
ATeDe
Posted
Updated 7-Feb-18 23:28pm

Here is one way...
VB
Dim A As String() = {"a", "b", "c", "d", "e", "f", "g", "h", "i"}
Dim B As String() = {"b", "d", "e"}

Dim c = A.Select(Function(x, i) New KeyValuePair(Of String, Integer)(x, i)) _
         .Where(Function(x) B.Contains(x.Key))

For Each item In c
    Console.WriteLine("{0} - {1}", item.Key, item.Value)
Next
 
Share this answer
 
Comments
Maciej Los 8-Feb-18 5:28am    
5ed!
Graeme_Grant 8-Feb-18 5:30am    
Thanks
ATeDe 8-Feb-18 15:55pm    
Thanks Graeme, nice example of LINQ application. Actually you've given me indication that KeyValuePair can be used to convert array A to listA following way:

Dim listA As List(Of KeyValuePair(Of String, Integer)) = A.Select(Function(x, i) New KeyValuePair(Of String, Integer)(x, i)).ToList()
Graeme_Grant 8-Feb-18 16:26pm    
Yes... you could also use tuples...
ATeDe 8-Feb-18 17:39pm    
Yeah, and instead of using Dim A As String(), listA might be created directly, and then we got very neat statement:

Dim C = listA.Where(Function(x) B.Contains(x.Key))
For Each item In C
Console.WriteLine("{0} {1}", item.Key, item.Value)
Next
As an alternative to solution#1 of Graeme_Grant[^], a "translated" (to Linq query) version of your solution is:

VB.NET
Dim A As String() = {"a", "b", "c", "d", "e", "f", "g", "h", "i"}
Dim B As String() = {"b", "d", "e"}

Dim result = A.Where(Function(q) B.Any(Function(w) q=w)). _
	Select(Function(x) New With _
		{ _
			.Index = Array.IndexOf(A, x), _
			.Value = x _
		})


I'd suggest to check the following MSDN documentation for more details:
Enumerable.Intersect(TSource) Method (IEnumerable(TSource), IEnumerable(TSource)) (System.Linq)[^]
Enumerable.Except(TSource) Method (IEnumerable(TSource), IEnumerable(TSource)) (System.Linq)[^]
Enumerable.ElementAt(TSource) Method (IEnumerable(TSource), Int32) (System.Linq)[^]
 
Share this answer
 
v3
Comments
Graeme_Grant 8-Feb-18 5:32am    
5'ed the direct translation :)
Maciej Los 8-Feb-18 5:35am    
Thank you, Graeme
Richard Deeming 8-Feb-18 11:30am    
That's not really a direct translation, though. :)

The original code iterates through B, finding the index in A for each item. Your version starts with A, filters it to items that exist in B, and then finds the index in A.

A real translation would be:
Dim result = B.Select(Function (x) New With
{
    .Value = x,
    .Index = Array.IndexOf(A, x)
})
Maciej Los 8-Feb-18 11:58am    
OK. It's almost direct translation ;)
Cheers!
Maciej
ATeDe 8-Feb-18 15:59pm    
Great stuff, Richard! Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900