Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
If you want to be more Linq-like without creating a ForEach method: vehicles = vehicles .Select( v => new Vehicle { Make = v.Make, ...
Answer
#2: Post edited
- If you want to be more Linq-like without creating a `ForEach` method:
- ````
- vehicles = vehicles
- .Select(
- v =>
- new Vehicle
- {
- Make = v.Make,
- VIN = v.VIN,
- Color = v.Color,
- HsRegistration = vinList.Contains(v.VIN)
- }
- ).ToList();
- ````
Here we're treating `Vehicle` and the list like immutable objects, and creating modified copies rather than mutating them.
- If you want to be more Linq-like without creating a `ForEach` method:
- ````
- vehicles = vehicles
- .Select(
- v =>
- new Vehicle
- {
- Make = v.Make,
- VIN = v.VIN,
- Color = v.Color,
- HsRegistration = vinList.Contains(v.VIN)
- }
- ).ToList();
- ````
- Here we're treating `Vehicle` and the list like immutable objects, and creating modified copies rather than mutating them.
- Can you make Vehicle a `record` rather than a `class`?
- ````
- record VehicleRecord
- {
- internal string Make;
- internal string VIN;
- internal string Color;
- internal bool HasRegistration;
- }
- ````
- If so you can simplify this to:
- ````
- vehicles = vehicles
- .Select(
- v => v with { HasRegistration = vinList.Contains(v.VIN) }
- ).ToList();
- ````
#1: Initial revision
If you want to be more Linq-like without creating a `ForEach` method: ```` vehicles = vehicles .Select( v => new Vehicle { Make = v.Make, VIN = v.VIN, Color = v.Color, HsRegistration = vinList.Contains(v.VIN) } ).ToList(); ```` Here we're treating `Vehicle` and the list like immutable objects, and creating modified copies rather than mutating them.