Filtering a collectiion
Hi
I am using the following code to filter entries.
public CollectionEx Filter(CollectionEx pList, String pPropertyName, object pFilterValue)
{
CollectionEx filteredList = new CollectionEx();
foreach (T item in pList)
{
Type itemType = item.GetType();
PropertyInfo propertyInfo = item.GetType().GetProperty(pPropertyName);
object valueOfX = propertyInfo.GetValue(item, null);
if (valueOfX == pFilterValue)
{
filteredList.Add(item);
}
}
return filteredList;
}
Even in situations where the objects are equal, this statement always evaluates to false.
if (valueOfX == pFilterValue)
Where am i going wrong?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Sep 3, 2007, 5:33 AM EDT
When you say it returns false even when the objects are 'equal', do you mean that the operands of the == operator are references to the same object, or that the objects are 'equal' in some other sense - for example, in the case of strings, that they contain the same characters in the same order?
manK3 reputationPosted Sep 3, 2007, 7:13 AM EDT
AlanRank #669 · 3.6K reputationPosted Sep 3, 2007, 9:03 AM EDT
See if it makes any difference if you use the virtual Equals() method rather than the == operator:
if (pFilterValue.Equals(valueOfX))