user380432
user380432

Reputation: 4779

C#/LINQ iterating through object in date order

I have a driver list object(_currentDriverData) that contains multiple unique drivers. Then I have a data list object (_logsDutyStatusChange)that contains data for these drivers.

The _logsDutyStatusChange object contains multiple records for each driver. I need to go through the data records 1 by one in date order matching on driver id.

The _logsDutyStatusChange object contains a date field, a driverid field and some data fields.

I need to first find what drivers are in the _logsDutyStatusChange object, then I need to assign certain values to the _currntDriverData object according to the data.

How would I go through these objects 1 by 1 in date order?

I have the following so far:

var lookup = _logsDutyStatusChange.ToLookup(x => x.did);
                    foreach(CurrentDriverInfo driver in _currentDriverData)
                    {
                         if (lookup.Contains(driver.driverid))
                         {

Upvotes: 0

Views: 209

Answers (2)

Ian Newson
Ian Newson

Reputation: 7949

I think you need something like this inside your if statement:

foreach (var log in lookup[driver.driverid].OrderBy(log => log.date))
{

}

Upvotes: 2

Craig Eddy
Craig Eddy

Reputation: 959

foreach( var x in _logDutyStatus.OrderBy( lds => lds.datefield ) )
{
    var driver = _currentDriverData.FirstOrDefault( d => d.driverid = x.did );
    if( driver != null )
    { 
        // do stuff with driver 
    }
}

Upvotes: 0

Related Questions