Our Blog

Optimize your code using Linq-to-SQL ToDictionary()

Lyubomir Lyubenov
by Lyubomir Lyubenov on Mon 10 February 2014 No comments

The advantages of using Linq into a Dictionary for cashing and its power for retrieving a value by using its key are well-known. In short, the Dictionary<TKey, TValue> is a generic class that provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary<TKey, TValue> class is implemented as a hash table.

I’ll present my case where ToDictionary() optimize my code. I have to manage two objects: hierarchies and levels. I am using Rest service (Asp.Net MVC – Web API) to send JSON data and jQuery.ajax() request to retrieve it. In this example the objects have the following fields:

Hierarchies:

  • UniqueID – unique GUID number of hierarchy.
  • Name – name of hierarchy.
  • ChildrenLevelID – unique GUID number equal to UniqueID of level’s object.
  • ChildrenCount – count of children objects.
  • Children – children objects for the current hierarchy object.

Levels:

  • Name – name of Levels.
  • UniqueID – unique GUID number of levels.

Linq-to-SQL ToDictionary

Every level in Hierarchy objects has equal ChildrenLevelID. For example:

Level1 (Levels.Name: Countries) – Bulgaria and Netherlands have Children.ChildrenLevelID=Levels.UniqueID = 26393a77-fa59-4ec9-b1f9-ecb2bed19f0a

Level2 (Levels.Name: Cities) – Sofia and Amsterdam have Children.ChildrenLevelID = Levels.UniqueID = cd40fddb-4b5d-4e16-8009-ee4b912eeeb2

The first wrong approach to process hierarchies and levels data without using Linq-To-SQL ToDictionary() is to send these objects separately in the Rest service and the Levels data (UniqueID and Name) to be sent also separately. Then we have to find one object from Hierarchies and find  its level name. In this case we have to loop all levels from Levels object and compare Levels.UniqueID with Hierachies.ChildrenLevelID.


// Rest service: return JSON object without ToDictionory()
var result = new
{
    Hierarchies =  context.Hierarchies.ToList(),
    Levels = context.Levels.ToList()
};
return result;

 

// JavaScript: loop every object from levels to find hierarchy level.
var level = getLevel(Levels, Hierarchies[0].ChildrenLevelID )
function getLevel(levels, UniqueID) {
     for (var prop in levels) {
           if (levels[prop].UniqueID == UniqueID) {
               return levels[prop];
           }
     }
}

The second approach to process hierarchies and levels data is to use Linq-To-SQL ToDictionary(). We send Levels objects as a pair of key and value. The key is Unique number and the value is level’s name. In this case we don’t need to loop every Levels object to find our hierarchy level. Now our request is optimized to run faster with less code.

// Rest service: return JSON object with ToDictionory()
var result = new
{
     Hierarchies =  context.Hierarchies.ToList(),
     Levels = context.Levels.ToList(l=>l.UniqueID, l=>l.Name)
};
return result;

 

// JavaScript: Get level’s value from key.
var level = Levels[Hierarchies[0].ChildrenLevelID];

Linq-to-SQL ToDictionary1

Lyubomir LyubenovOptimize your code using Linq-to-SQL ToDictionary()

Related Posts

Take a look at these posts

Join the conversation