Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching elements within list

Tags:

c#

.net

csv

I want to search elements within a list:

public static string FindOne(string vehicleRego)
{
    string vehicleDetails = 
        Fleet.vehicleList.Find(x => x.VehicleRego == vehicleRego).ToString();

    return vehicleDetails;
}

But I'm not having any luck - I'm not sure how to get the whole 'row' and also how to just return one of the items in the object, say 'model'.

like image 791
hylian Avatar asked May 14 '26 01:05

hylian


1 Answers

vehicleList is of type List<Vehicle> and Fleet.vehicleList.Find returns first match, so the return type is Vehicle

You could return the found Vehicle - this is the whole "row". If not found, null is returned.

public static Vehicle FindOneByRego(string vehicleRego)
{
    return Fleet.vehicleList.Find(vehicle => vehicle.VehicleRego == vehicleRego);
}
like image 143
Hawk Avatar answered May 15 '26 15:05

Hawk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!