Click here to Skip to main content
15,914,594 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
I have a binding list contains with objects. That object can be like this.

Customers cust=new Customers();


It has this properties.
cust.Id - string
cust.Name - string
And there is a property with customer addresses.For that I use another list that contains list of address objects and keep it in cust.Address property. Address class has following properties.
Street,
Po Box.
Country,
AddressType (eg. Bussiness, Home, Other)
I have implemented the Find method to find in BindingList. Now I can use find method to get customer details using customer object's properties.
But how can I use this method to get customer details using Address object's properties. (eg. address.PoBox="111")
Implemented find method like this.
protected override int FindCore(PropertyDescriptor property, object key)     
 {
            // Get the property info for the specified property.
            PropertyInfo propInfo = typeof(T).GetProperty(property.Name);
            T item;
            if (key != null)
            {
                // Loop through the items to see if the key
                // value matches the property value.
                for (int i = 0; i < Count; ++i)
                {
                    item = (T)Items[i];
                    if (propInfo.GetValue(item, null).Equals(key))
                        return i;
                }
            }
            return -1;
        }
Posted

1 solution

I'm not sure why you are using reflection to do this job. But still if you have to use it then try below steps.
For properties at nth level (Customer.Addresses[index]."Some Property"):
1. Pass the property path (i.e. "customer.addresses.Name") to your current method instead of property descriptor.
2. Now for each property (split property path on ".") get the object.
3. If the object is not of collection type, move to the next property.
4. If the object is of collection type, get the count and loop for each item and check the next property.
5. Repeat step 2 - 4, till all properties are traversed.
6. Now compare the value of the object found in step 5 with the value you are looking for.

I'm not able to post code as of now but I can get you one when I've time. If you want to consider LINQ then let me know.
 
Share this answer
 
Comments
RakeshMeena 27-May-11 2:55am    
Below is the LINQ code for your problem:

from cust in customers
where cust.Addresses.Exists(add => add.City == "Test Value")
select cust;

Here "customers" is the item collection.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900