title: Serializing result of a LINQ IEnumerable
body: <p>I have a simple value type:</p>
  
  <pre><code>    [Serializable]
      private struct TimerInstance
      {
          public TimerInstance(string str, long nTicks)
          {
              _name = str;
              _ticks = nTicks;
          }
  
          private readonly string _name;
          private readonly long _ticks;
  
          public string Name { get { return _name; } }
          public long Ticks { get { return _ticks; } }
  
          public override string ToString()
          {
              return string.Format("{0,20}: {1,10:N}", Name, Ticks);
          }
      }
  </code></pre>
  
  <p>which as you'll note is serializable.  Then I have a list of these:</p>
  
  <pre><code>static private List&lt;TimerInstance&gt; _Timers = new List&lt;TimerInstance&gt;();
  </code></pre>
  
  <p>and a LINQ method to eliminate the bottom 5% and top 5% of timers from the list:</p>
  
  <pre><code>// Return items that should be persisted.  By convention, we are eliminating the "outlier"
  // values which I've defined as the top and bottom 5% of timer values.
  private static IEnumerable&lt;TimerInstance&gt; ItemsToPersist()
  {
      // Eliminate top and bottom 5% of timers from the enumeration.  Figure out how many items
      // to skip on both ends.
      int iFivePercentOfTimers = _Timers.Count / 20;
      int iNinetyPercentOfTimers = _Timers.Count - iFivePercentOfTimers * 2;
  
      return (from x in _Timers
              orderby x.Ticks descending
              select x).Skip(iFivePercentOfTimers).Take(iNinetyPercentOfTimers);
  }
  </code></pre>
  
  <p>I then am trying to Seralize to XML the result of this enumeration, i.e. serialize just the values of the timers in the middle 90%, eliminating the top and bottom 5%:</p>
  
  <pre><code>// Serialize the timer list as XML to a stream - for storing in an Azure Blob
  public static void SerializeTimersToStream(Stream s)
  {
      BinaryFormatter f = new BinaryFormatter();
      f.Serialize(s, ItemsToPersist());
  }
  </code></pre>
  
  <p>The problem is that when this code executes, I get this:</p>
  
  <blockquote>
    <p>A first chance exception of type
    'System.Runtime.Serialization.SerializationException' occurred in
    mscorlib.dll
        Microsoft.WindowsAzure.ServiceRuntime Critical: 1 : Unhandled Exception:
        System.Runtime.Serialization.SerializationException: Type 'System.Linq.Enumerable+d__3a`1[[TracePerfWorker.TraceTimer+TimerInstance,
    TracePerfWorker, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=null]]' in Assembly 'System.Core, Version=4.0.0.0,
    Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as
    serializable.
           at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType
    type)
           at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type
    type, StreamingContext context)
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object
    obj, ISurrogateSelector surrogateSelector, StreamingContext context,
    SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
    ObjectWriter objectWriter, SerializationBinder binder)
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object
    obj, ISurrogateSelector surrogateSelector, StreamingContext context,
    SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
    ObjectWriter objectWriter, SerializationBinder binder)
           at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object
    graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream
    serializationStream, Object graph, Header[] headers, Boolean fCheck)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream
    serializationStream, Object graph)
           at TracePerfWorker.TraceTimer.SerializeTimersToStream(Stream s) in c:\Users\Mike\Documents\Visual Studio
    2010\Projects\AzureTracePerfTest\TracePerfWorker\TraceTimer.cs:line 88
           at TracePerfWorker.WorkerRole.SerializeTimersToBlob(String strTimerGroupName) in c:\Users\Mike\Documents\Visual Studio
    2010\Projects\AzureTracePerfTest\TracePerfWorker\WorkerRole.cs:line
    192
           at TracePerfWorker.WorkerRole.DoWorkNoTrace() in c:\Users\Mike\Documents\Visual Studio
    2010\Projects\AzureTracePerfTest\TracePerfWorker\WorkerRole.cs:line
    153
           at TracePerfWorker.WorkerRole.Run() in c:\Users\Mike\Documents\Visual Studio
    2010\Projects\AzureTracePerfTest\TracePerfWorker\WorkerRole.cs:line 77
           at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal()
           at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRole()
           at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.b__1()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean
    ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()</p>
  </blockquote>
  
  <p>I think I get what this is telling me - the implicit class that the enumerator has apparently generated ('System.Linq.Enumerable+d__3a`1[[TracePerfWorker.TraceTimer+TimerInstance, TracePerfWorker') is not itself marked as serializable.</p>
  
  <p>But this seems like a really common situation, where I'm taking a serializable value type 
  (TimerInstance), and just building a LINQ query over a list of these values, i.e. the enumerator is just returning TimerInstance values - how do I then convince it that what the enumerator is returning is just a list of TimerInstance values, which are serializable?</p>