views:

49

answers:

1

I'm using EntityFrameowrk 4 and WinForms. I have a DataGridView which shows 5 to 12 thousands of records. The records are from different levels of hierarchy. There are records of classes A, B, C, D, where A contains a collection of B, B contains a collection of C, etc. The most numerous class is D. In the DataGridView this hierarchy is flattened and each record is shown in the row with some color/font formatting. To do this type of rendering I wrote the following code:

foreach (a in A)
{
   var displayObjectA = new DisplayObject()
   {
      Name = a.Name,
      Code = "Section", 
      Object = a  
   }
   data.Add(displayObjectA);

   var B = a.B;

   foreach (b in B)
   {
      var displayObjectB = new DisplayObject()
      {
         Name = b.Name,
         Code = "Subsection", 
         Object = b  
      }
      data.Add(displayObjectB);

      var C = b.C;

      foreach (c in C)
      {
         var displayObjectC = new DisplayObject()
         {
            Name = c.Name,
            Code = "Group", 
            Object = c 
         }
         data.Add(displayObjectC);

         var D = c.D; 
         foreach (d in D)
         {
            var displayObjectD = new DisplayObject()
            {
               Name = d.Name,
               Code = d.Code, 
               Object = d 
            }
            data.Add(displayObjectD);
         }
      }
   }
}

after that the data is dispayed in the DataGridView. The problem is that this rendering is taking too long. And the most time consuming part seems to be the line

foreach (d in D)

in particular the call to a function

System.Data.Objects.DataClasses.EntityCollection<T>.GetEnumerator(). 

When I looked at this function in Reflector it shows that it's body is empty.

My questions are:

  1. Is there a better way to do this type of rendering?
  2. How do I increase the performance?
  3. Why is the GetEnumerator() empty? Is it constructed at run-time?
+3  A: 
  1. Use paging as Darin suggested.
  2. Make sure you are loading all the inner collections using .Include() and don't use lazy loading. Otherwise every access to the child collection of an entity will do an etxra request to the DB. And again, use paging.
  3. You're probably using .NET 4 and looking at the Program Files\Reference Assemblies folder. These are just empty assemblies only containing metadata. The real ones are in Windows\Microsoft.NET. Or upgrade to Reflector 6.1 which is able to handle the mapping between the two.
Julien Lebosquain