Q:

Sorted-ception - Adding to a SortedList inside a SortedDictionary

So this is a thing I've been looking at for quite some time now. And I can't see where I did it wrong. Hope you guys can help ^^
So my problem is that I have 311 objects which I try to sort into a SortedDictionary<int, SortedList<int, Entry>> (>). However the result is a dictionary with only 112 objects. Where do the rest go and why ain't they going where the should?
    public SortedDictionary<int, SortedList<int, Entry>> GetSortedByForum(int id)
    {
        SortedDictionary<int, SortedList<int, Entry>> result = new SortedDictionary<int, SortedList<int, Entry>>();

        foreach (var e in GetByForum(id))
        {
            e.fk_entry = e.fk_entry == null
                ? 0
                : e.fk_entry;

            if (!result.ContainsKey((int)e.fk_entry))
                result[(int)e.fk_entry] = new SortedList<int, Entry>();

            if (!result[(int)e.fk_entry].ContainsKey(e.fk_language))
                result[(int)e.fk_entry][e.fk_language] = new Entry();

            result[(int)e.fk_entry][e.fk_language] = e;
        }

        return result;
    }

Background info might help:

fk_entry is the objects parent. An entry can only have on parent, but can however have multiple children.
fk_language is the language of the entry, a entry can have several translations
entry is an article of some sort. The dictionary should order them by parent, and then by language.

A:

Your problem is in the following line of code. It only adds the first entry for a particular language - later entries for the same parent and language are not added.
if (!result[(int)e.fk_entry].ContainsKey(e.fk_language))
    result[(int)e.fk_entry][e.fk_language] = new Entry();

Keeping as much of your code the same as possible, I think you want a collection of entries associated with each particular language:
var result = new SortedDictionary<int, SortedList<int, List<Entry>>>();

...
...

if (!result[(int)e.fk_entry].ContainsKey(e.fk_language))
    result[(int)e.fk_entry][e.fk_language] = new List<Entry>();

result[(int)e.fk_entry][e.fk_language].Add(e);

Given your comment, "How would I do it if I wanted to also sort the different translations by fk_language?", you could try incorporating something like this into your loop:
var result2 = new SortedDictionary<int, Dictionary<int, List<int>>>();

if (!result2[(int)e.fk_entry].ContainsKey(e.some_unique_entry_id))
    result2[(int)e.fk_entry][e.some_unique_entry_id] = new List<int>();

result2[(int)e.fk_entry][e.some_unique_entry_id].Add(e.fk_language);

Or use LINQ to query the first SortedDictionary, to manipulate it into the format you need.

