Is there any way to clean up this type of loop using LINQ?
List<Car> result; List<string> makes; List<string> models; for (int i = 0; i < makes.Count() && i < models.Count(); i++) { result.Add(new Car() { Make = makes[i], Model = models[i] }); }
Basically I’m looking for some way to collate multiple arrays of individual fields into a single array of objects made up of those fields.
You could use
Enumerable.Range, like so:If
makesandmodelsalways contain the same number of items, you can use more compact syntax: