I have a datareader which has columns like this among other columns populated via an SP:
ColX ColY ColZ
NYC 100 200
NYC 101 200
EWR 100 200
EWR 100 200
I'm trying to iterate over this result, and perform an action like sending an email for every duplicate in the combination of ColX, ColY, ColZ; in the above example EWR-100-200
What is the efficient way to accomplish this given that I have no control over the SP that returns this data in C#?
Create a Hashset<string>
to hold keys for the items you've sent emails to. You can create the keys by:
string key = ColX.ToString() + ColY.ToString() + ColZ.ToString();
So:
var keysUsed = new Hashset<string>();
while (reader.Read())
{
// Assuming you got the values from the columns...
string key = ColX.ToString() + ColY.ToString() + ColZ.ToString();
if (keysUsed.Add(key))
{
// send email
}
}
The point here is that keysUsed.Add
will return true
if the item is added to the Hashset
. If the key already existed, the method would return false
.