I have a Car
object with a ResaleValue
property, and I have a collection of these car objects stored in:
IEnumerable<Car>
I also have a ResaleCalculator() with a calculate method.
Is there a way in linq to apply a calculation and set the ResaleValue
property of every object in the collection without a loop?
You don't really want to use LINQ for this. Firstly, you're not avoiding a loop, you are merely abstracting it away. Secondly, LINQ methods are intended to filter and/or project a sequence, not mutate it. While you could use the .ForEach
instance method on List<T>
to not explicitly write a loop, it is hardly clearer than simply coding the loop to do what you need it to do.
I think this will do what you want
cars.Select(c=>c.ResaleValue = c.Calculate());
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