Suppose this is what I have in my database
table Ancestor (
idAncestor int not null,
name varchar(20) not null,
)
table Descendant (
idDescendant int not null,
name varchar(20) not null,
Ancestor_idAncestor int not null
)
When ADO.NET generates the entity object for the above 2 tables, I can access Descendant
of Ancestor
through Ancestors.First().Descendants
.
If I were to recursively iterate over an ancestor's descendant(s) or descendant's descendant(s) and print out its id
and name
, the following is my attempt
public void Iterate(Ancestor a)
{
Type t = a.GetType();
PropertyInfo[] props = t.GetProperties();
foreach(var prop in props){
// pseudo code here
if(prop is entitycollection)
{
// how do I convert prop to entity collection here??
foreach(var p in prop){
Iterate(p)
}
} else {
print prop.GetValue(a, null)
}
}
}
My problem is trying to figure out if a entity property is of entity collection, if it is, then find the type hold by the collection, then iterate over the type, and so on.
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…