Filter an array with LINQ in VB

  • Thread starter Thread starter PsychoPif
  • Start date Start date
P

PsychoPif

Hello,
I've heard that LINQ can do pretty much the same thing I could do if I
could run an SQL query on my collection. I'd like to do something like
this:

Let's say I have and array of object of type TypeOne
The TypeOne has a property called id of type string
I have an array that contain thousands of string that represent ids of
TypeOne object.
I'd like to filter my array so that only those that their id property
match with one of those in my array of string be returned.

At first, I tried doing something like :

dim filteredArray as TypeOne()
filteredArray = From a in ArrayOfOnes Join i in ArrayOfIDs On a.id = i

but I have not found a way to return an array of TypeOne. I get a cast
exception saying I cannot cast a join iterator to a TypeOne()

I tried
filteredArray = From a in ArrayOfOnes From i in ArrayOfIDs Where a.id
= i

I get another cast error.

As you can probably see, I'm a real beginner with LINQ but I'd really
like to learn it. Is there a way to filter my array and get back a
strongly typed array with LINQ?
 
Just in case someone wonder I would do it like this in SQL
SELECT TypeOnes.* FROM TypeOneCollection INNER JOIN IDs ON IDs.theID =
TypeOnes.ID
 
Back
Top