SkyeBoniwell
SkyeBoniwell

Reputation: 7102

return objects of a specific type in linq query

I'm trying to write a linq query that returns all objects of a certain type.

Would something like this work if I wanted to get all the cars that are sedans?

Dim cars = From c In carFactory.GetAllcars()
           Select car = carFactory.GetAuto(c.ID)
           Where TypeOf (car) Is Sedan

Upvotes: 0

Views: 149

Answers (2)

I4V
I4V

Reputation: 35353

 carFactory.GetAllcars().OfType<Sedan>().Select(car=>car.ID)

Upvotes: 2

SLaks
SLaks

Reputation: 887469

That will work, but the return type will still be IEnumerable(Of Car).

Instead, you can write sequence.OfType(Of Sedan)(); this will filter out non-sedans and return an IEnumerable(Of Sedan).

Upvotes: 5

Related Questions