Filter an array with LINQ in VB

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PsychoPif

    Filter an array with LINQ in VB

    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?
  • PsychoPif

    #2
    Re: Filter an array with LINQ in VB

    Just in case someone wonder I would do it like this in SQL
    SELECT TypeOnes.* FROM TypeOneCollecti on INNER JOIN IDs ON IDs.theID =
    TypeOnes.ID

    Comment

    Working...