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?
AlanPosted Sep 3, 2007, 1:03 PM
See if it makes any difference if you use the virtual Equals() method rather than the == operator:
if (pFilterValue.Equals(valueOfX))
manKPosted Sep 3, 2007, 11:13 AM
AlanPosted Sep 3, 2007, 9:33 AM
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?