Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# foreach only get distinct values

Tags:

c#

I have the following code:

foreach (Logs log in _logsDutyStatusChange)
{
    string driverid = log.did;
}

How would I only add distinct driver ID's to the driverid string?

Thanks

like image 724
user380432 Avatar asked Apr 18 '11 14:04

user380432


1 Answers

This should work (without linq):

Hashset<string> alreadyEncountered = new Hashset<string>();
foreach (Logs log in _logsDutyStatusChange)
{
    if(alreadyEncountered.Contains(log.did))
    {
        continue;
    }
    string driverid = log.did;
    alreadyEncountered.Add(driverid);
}
like image 72
Victor Hurdugaci Avatar answered Sep 27 '22 15:09

Victor Hurdugaci