Solved

C#/Linq/SQLite Recursive query to build a family tree

Posted on 2012-12-21
3
1,461 Views
Last Modified: 2013-01-14
I have a table of animals.  The important fields are:

animal_id
mother_animal_id
father_animal_id

mother_animal_id and father_animal_id both relate back to animal.animal_id.

I need to build a result set of an animal and it's ancestors.  Each set of immediate ancestors will have a higher level number.  I would stop at a maximum of 6 levels above the base animal.

For example, this is what a small dataset might look like:

animal_id, mother_animal_id, father_animal_id, level
104, 98, 97, 0
98, 101, 102, 1
97, 4, 10, 1
101, 60, 44, 2
102, 11, 6, 2
4, 57, 22, 2
10, 66, 21, 2

I'm using SQLite, so I don't have the ability to use a stored procedure.  Can I do this with LINQ?  If so, how?

TIA
0
Comment
Question by:rip55jcp
3 Comments
 
LVL 42

Accepted Solution

by:
EugeneZ earned 500 total points
ID: 38715578
0
 
LVL 18

Expert Comment

by:Gary Davis
ID: 38715798
This solution uses recursion and Linq. I developed with LinqPad which has that Dump() function so replace that with your own output. Also, you can sort the output by Level if needed.

Gary Davis

const int MaxLevel = 6;	// Maximum depth
List<Animal> animals = new List<Animal>();		// Return
List<Animal> animalData = new List<Animal> {	// Database
		new Animal (1, 98, 97, 0),
		new Animal (2, 98, 97, 0),
		new Animal (3, 98, 97, 0),
		new Animal (104, 98, 97, 0),
		new Animal (98, 101, 102, 1),
		new Animal (97, 4, 10, 1),
		new Animal (101, 60, 44, 2),
		new Animal (102, 11, 6, 2),
		new Animal (4, 57, 22, 2),
		new Animal (10, 66, 21, 2)};

void Main()
{
	GetAnimal(104);	
	animals.Dump();
}

// Add one animal record to the list of ancestors. Recursively called.
void GetAnimal(int id)
{
	var animal = animalData.SingleOrDefault(s => s.AnimalId == id);
	if (animal == null) return;	// Stop recursion (not found)
	animals.Add(animal);
	if (animal.Level == MaxLevel) return;	// Stop recursion (max level)
	GetAnimal(animal.MotherAnimalId);
	GetAnimal(animal.FatherAnimalId);	
}

public class Animal
{
	public Animal(int animalId, int motherAnimalId, int fatherAnimalId, int level) // Constructor
	{
		AnimalId = animalId;
		MotherAnimalId = motherAnimalId;
		FatherAnimalId = fatherAnimalId;
		Level = level;
	}
	
	public int AnimalId {get;set;}
	public int MotherAnimalId {get;set;}
	public int FatherAnimalId {get;set;}
	public int Level {get;set;}
}

Open in new window

AnimalsResult.png
0
 

Author Comment

by:rip55jcp
ID: 38717020
I'm accepting the comment made by EugeneZ as the correct answer since the links led me to an acceptable solution.  Gardavis, yours was close, but the level value is not part of the original table.  It has to be incremented for each level of ancestors found.  

Thanks!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question