Q:

Checking if object exists then setting it based on rules

I have 2 business objects, trip and driver.
Each contain a driver ID and a driver date (MM/DD/YYYY)
Trip can have multiple of the same driver.driverid's in it. I need to get the closest instance of the driver.driverid in trips to the driver I am currently in.
So for example I have the folling so far:
var lookup = driver.driverid; 

This is the current driver I am looking at (using a foreach to go through the drivers object 1 by 1)
I now need to say get the most recent trip object that has the same driverid and the closest but not equal past date. If there is not one that exists, then exit the method.
Thanks

A:

Using LINQ you could do something like this (assuming your object structure);
 var selectedTrip = trips.Where(x => x.DriverId == desiredDriverId && x.TripDate > desiredDate).OrderBy(x => x.TripDate).FirstOrDefault();

 if(selectedTrip == null)
    return;

 // selectedTrip is your desired object

