开发者

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.dr开发者_如何学编程iverid 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜