c# - Return list of parents with the object -


i using entity framework , linq , trying return array of parents without using loop. here example 1 facing (i can't give real example same thing). there person , when retrieve person, want array of person's ancestors (male only). return , array [father, grandfather, great-grandfather,...] until "father" property null, or in other words don't know person's father is. best way entity framework?

here's example:

class person() {     public string name { get; set; }     public guid fatherid { get; set; }     public virtual person father { get; set; } }  class persondto() {     public string name { get; set; }     public ienumerable<persondto> ancestors { get; set; } } 

how loop is:

person person = context.people.find(personid); persondto persondto = person.todto();  person father = person.father;  while (father != null) {     persondto.ancestors.add(father.todto());     father = father.father; } 

assuming there foreign key on person table references father (within same table), , using sql server persistence, best off using stored procedure containing common table expression this.

http://blogs.msdn.com/b/simonince/archive/2007/10/17/hierarchies-with-common-table-expressions.aspx

if use ef run sp, , set import returning set of "person", should find (as added bonus) parents set on objects.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -