This is simple, please just add the if ... else ... loop as the filter. If you need the average, you will have the PIValues object to store the results. Now, you could consider following:
foreach (PIValue value in values)
Thanks Wang it worked perfectly.