I'm not sure that title conveys my meaning, please change it if you can better define this question in a sentence.
I'd like to iterate through a collection of T, but instead of working with the (T)object, I want to retrieve (T)object.SomeProperty.
How I can do it:
List<Car> _cars;
FillList(_cars);
foreach (Car car in _cars)
{
Windshield ws = car.Windshield;
ws.DoWorkA();
ws.DoWorkB();
ws.DoWorkC();
}
What I'd love to do:
List<Car> _cars;
FillList(_cars);
foreach (Car car in _cars using ws as Windshield = car.Windshield)
{
ws.DoWorkA();
ws.DoWorkB();
ws.DoWorkC();
}
Any way to become an even lazier programmer?
You can use LINQ and select:
foreach (Windshield ws in _cars.Select(car => car.Windshield))
{
ws.DoWorkA();
ws.DoWorkB();
ws.DoWorkC();
}
use Linq:
List<Car> _cars;
FillList(_cars);
foreach (Windshield ws in _cars.Select(c=>c.Windshield) {
ws.DoWorkA();
ws.DoWorkB();
ws.DoWorkC();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With