I have a collection:
List<Car> cars = new List<Car>();
Cars are uniquely identified by their property CarCode
.
I have three cars in the collection, and two with identical CarCodes.
How can I use LINQ to convert this collection to Cars with unique CarCodes?
You can use grouping, and get the first car from each group:
List<Car> distinct = cars .GroupBy(car => car.CarCode) .Select(g => g.First()) .ToList();
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