title: Serialize Linq Results directly to JSON
body: <p>I'm developing a WebService that excecute linq to sql db and put the results into a VAR variable. Then I wanna serialize the result inside VAR to json format using javascript serializer (c#). Something like this:</p>
  
  <pre><code>var sb= from p in ent.people .........
  System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new       System.Runtime.Serialization.Json.DataContractJsonSerializer(sb.GetType());
  MemoryStream ms = new MemoryStream();
  serializer.WriteObject(ms, sb);
  string json = System.Text.Encoding.Default.GetString(ms.ToArray());
  </code></pre>
  
  <p>BUT I GET AN ERROR RESPONSE LIKE THIS:</p>
  
  <pre><code>Type      &amp;#39;System.Data.Objects.ObjectQuery`1[&amp;lt;&amp;gt;f__AnonymousType2d`5[System.String,System.Nu llable`1[System.Int32],System.Nullable`1[System.Int32],System.Int32,System.String]]&amp;#39; cannot be serialized. 
  </code></pre>
  
  <p>Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.</p>
  
  <p>HOW CAN I SERIALIZE LINQ RESULTS DIRECTLY TO JSON??
  Thanks a lot for all answers!
  Enrico</p>