Solved

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

Posted on 2012-12-21
3
1,433 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
postgres queries -- need opinions 1 36
Chat Room 1 26
SQL anywhere 11 databases 1 31
COnsume rest client 6 9
Creating and Managing Databases with phpMyAdmin in cPanel.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now