Hi telldurges,
Please refer below code example.
Code
IEnumerable<Customer> items = new Customer[]
{
new Customer{ ID=1, Name="Mudassar khan", Countries =new List<Country>{new Country{ID=1,Name="India"},new Country{ID=2,Name="USA"}}},
new Customer{ ID=2, Name="Robert", Countries =new List<Country>{new Country{ID=2,Name="Mexico"},new Country{ID=2,Name="Canada"}}},
new Customer{ ID=3, Name="Rousay", Countries =new List<Country>{new Country{ID=3,Name="Germany"},new Country{ID=2,Name="France"}}},
};
foreach (Customer item in items)
{
PropertyInfo[] properties = typeof(Customer).GetProperties();
foreach (PropertyInfo property in properties)
{
if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
var itm = property.GetValue(item, null);
if (itm is ICollection)
{
var Count = ((ICollection)itm).Count;
foreach (Country country in ((ICollection)itm))
{
var Key = typeof(Country).GetProperties().Where(x => x.Name == "ID").FirstOrDefault().GetValue(country, null);
var value = typeof(Country).GetProperties().Where(x => x.Name == "Name").FirstOrDefault().GetValue(country, null);
}
}
}
}
}
public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public List<Country> Countries { get; set; }
}
public class Country
{
public int ID { get; set; }
public string Name { get; set; }
}
If you have any error revert back.