Using LINQ to query object hierarchies

I used LINQ to solve the following problem: find all titles of objects at hierarchy level X when you know object ID in hierarchy level Y. I cannot imagine if there is some other solution that is same short and clear as one that LINQ provides. Take a look and decide by yourself.

Here is simple diagram with my entities. Here are my simple rules. Level1 has no parent level and my contain one ore more Level2 entities. Level2 entities have one Level1 parent and one or more Level3 enitities. Level3 entities have one Level2 parent entity and collection of one or more Items. So there is many-to-many relationship between Level3 and Items.

Objects hierarchyWe cannot use composite pattern here because these classes will be very different and there will be no point where we need one common interface for them. That’s why we have one class per level. Also the number of levels is fixed and there is no plan to expand this hierarchy.

By the way, you can model arbitrary class hierarchies on this model and still use this example (as long as it doesn’t hurt performance and you are really sure what you are doing).

Excercise: having Level1 items collection and knowing Level3 item ID find all Items for specified Level3 item and return string of their titles separated by comma. As you don’t have access to source code of data source you must use IList<Level1> and LINQ.

We will use simple class structure given below and we expect that we already got list of Level1 items from some repository or data context.

public class Level1
{
   
public int Id { get; set
; }
   
public string Title { get; set
; }
   
public IList<Level2> Level2Items { get; set
; }
}

public class Level2
{
   
public int Id { get; set
; }
   
public string Title { get; set
; }
   
public Level1 Parent { get; set
; }
   
public IList<Level3> Level3Items { get; set
; }
}

public class Level3
{
   
public int Id { get; set
; }
   
public string Title { get; set
; }
   
public Level2 Parent { get; set
; }
   
public IList<Item> Items { get; set
; }
}

public class Item
{
   
public int Id { get; set
; }
   
public string Title { get; set; }
}

Now it’s time to write some hardcore loops and create a cool code-hell… or maybe it’s time to be elegant and use LINQ as stated before. Using LINQ we can provide the following solution:

public string GetItemsStringForLevel3(IList<Level1> level1Items, int level3Id)
{
   
var items = from l in
level1Items
               
from l2 in
l.Level2Items
               
from l3 in
l2.Level3Items
               
from p in
l3.Items
               
where
l3.Id == level3Id
               
select
p.Title;

   
return string.Join(", ", items.ToArray());
}

That’s it guys. As you can see you can use LINQ to query your data structures more easily than before. As always – check out if your solutions doesn’t affect security or performance of your system before you are saying that your task is done. :)

Update: Check out how to simulate IN Clause in LINQ queries!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    4 thoughts on “Using LINQ to query object hierarchies

    • September 23, 2009 at 5:01 pm
      Permalink

      Thanks this is the exact example I was looking for.

    • October 15, 2009 at 10:19 am
      Permalink

      Excellent sample, just what i was looking for.

    • February 6, 2011 at 9:00 am
      Permalink

      Excellent article, can we do the same with extression trees.

    • May 24, 2011 at 6:44 am
      Permalink

      Using linq to query object hierarchies.. I like it :)

    Leave a Reply

    Your email address will not be published. Required fields are marked *