Solved

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

Posted on 2012-12-21
3
1,529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Accepted Solution

by:
Eugene Z 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…

696 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