Merge entities which have the same children
Your code is very difficult to read.  (To give you an idea of how difficult it is, I've been looking at this for the past 2 hours so far)


You are using traditional for loops when you instead should be using foreach loops.
Your "loop variables" are poorly named.  Look toward the end of the code.  What is a c?  What is a d?  Leave the shorter names within smaller lambdas, index varialbes and possibly single instances of "utility" objects (e.g., StringBuilder or Regex).
You are trying too hard to write everything within a single line.  for loops, if conditions, large queries... all of them should span multiple lines.  With perhaps an exception to the if (duplicates.Count == 0) continue; line.  I'd be okay with that but you should try to keep those to a minimum (it might be possible that you could code it so that line is not needed).
You have a long chain of Append()s to the string builder when you really should use a single call to AppendFormat().
There are some snippets of your code which would work better either as a separate method or a LINQ query.  Use it.
LINQ to Entities should have made associations between your objects so your joins could be replaced by using the navigation properties.  If you don't have those set up, you should.
You also have some useless comments.  // For each class...  Do we really need a comment to tell us that?  Well, maybe since we were using for loops but that should have been avoided in the first place.


You have a lot of performance issues too.


You are calling ToList() on every query when you are simply just enumerating over it most of the time.
You have a lot of repeated loops on the same collection too.  You should be able to do a lot within a single loop.
Your query for finding duplicates will be a huge hit.  I don't have much to say on it right now but it's already making this O(n^2).
I have a feeling that you don't need the calls to LoadProperty().  From what I can tell, the majority of your code is filling in data manually when you probably don't need to.  Unfortunately I don't know the entity framework enough to know better (though I am looking into it right now).


Applying what I've explained to your code, I'd start with this (hopefully I didn't break anything).

var sb = new StringBuilder();
var counter = 0;
using (var ctx = new SchoolModel())
{
    var allClasses = ctx.Classes
        .Where(o =&gt; o.SubjectId &gt; 0)
        .OrderBy(o =&gt; o.Id)
        .ToList(); 

    foreach (var aClass in allClasses)
    {
        ctx.LoadProperty(aClass, o =&gt; o.Students);

        // a properly crafted query could remove this completely and not use LINQ to Objects
        var students = new HashSet&lt;int&gt;(aClass.Students.Select(s =&gt; s.Id));
        var dupClasses = allClasses
            .Where(o =&gt; o.SubjectId == aClass.SubjectId
                     &amp;&amp; o.Id != aClass.Id
                     &amp;&amp; students.SetEquals(o.Students.Select(s =&gt; s.Id)))
            .ToList();

        if (dupClasses.Count == 0) continue;

        ctx.LoadProperty(aClass, o =&gt; o.PeriodsTimetable);

        foreach (var dupClass in dupClasses)
        {
            // If the duplicate class has a timetable slot that's not also allocated to the
            // class we are keeping, then allocate it (otherwise MySql will delete it).
            ctx.LoadProperty(dupClass, o =&gt; o.PeriodsTimetable);

            foreach (var dupTimetable in dupClass.PeriodsTimetable)
            {
                var isDuplicate = aClass.PeriodsTimetable
                    .Any(ctp =&gt; dupTimetable.TeacherId == ctp.TeacherId
                             &amp;&amp; dupTimetable.PeriodTime == ctp.PeriodTime);
                if (!isDuplicate)
                    dupTimetable.ClassId = aClass.Id;
            }

            // Now update all other entities which reference the duplicate to instead reference
            // the class we are keeping.
            foreach (var dupPeriod in dupClass.Periods)
                dupPeriod.ClassId = aClass.Id;
            foreach (var dupAssessment in dupClass.Assessments)
                dupAssessment.ClassId = aClass.Id;
            foreach (var dupSeatingPlan in dupClass.SeatingPlans)
                dupSeatingPlan.ClassId = aClass.Id;
            foreach (var dupMerit in dupClass.Merits)
                dupMerit.ClassId = aClass.Id;

            sb.AppendFormat("{0} merged with {1}.&lt;br /&gt;", dupClass.LongName, aClass.LongName);

            // Finally, delete the duplicate
            ctx.Classes.DeleteObject(dupClass);

            // not sure this is really needed
            allClasses.Remove(dupClass);
            counter++;
        }
    }
    ctx.SaveChanges();
    Output = "&lt;b&gt;Merged " + counter + " duplicate classes.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;" + sb.ToString();
}


That's my initial analysis so far.  I'll update some more once I've gained understanding on what you are doing.  As I've said, it is very difficult to read.  I'll probably offer more improvements once I've had a chance to look at it more.