Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试编写一个返回某种类型的所有对象的 linq 查询。
如果我想得到所有的轿车,这样的事情会起作用吗?
Dim cars = From c In carFactory.GetAllcars() Select car = carFactory.GetAuto(c.ID) Where TypeOf (car) Is Sedan
这会起作用,但返回类型仍然是IEnumerable(Of Car).
IEnumerable(Of Car)
相反,你可以写sequence.OfType(Of Sedan)(); 这将过滤掉非轿车并返回一个IEnumerable(Of Sedan).
sequence.OfType(Of Sedan)()
IEnumerable(Of Sedan)
carFactory.GetAllcars().OfType<Sedan>().Select(car=>car.ID)