I have a set of vehicles in C #, and some cars are cars, and others are SUVs. I am wondering what is the best way to get the car with the lowest weight. If there is no car in the array, find the SUV with the lowest weight.
Below is the code segment I have LINQ. I am wondering if there is a better way to get this using one query instead of two queries.
Vehicle listVehicles[] = ... Vehicle lightestVehicle = (from aVehicle in listVehicles where aVehicle.Type == CAR orderby aVehicle.Weight ascending)?.FirstOrDefault(); if (null == lightestVehicle) lightestVehicle = (from aVehicle in listVehicles where aVehicle.Type == SUV orderby aVehicle.Weight ascending)?.FirstOrDefault(); return lightestVehicle;
Is there a better way to accomplish the same thing with groupby or another LINQ trick?
source share