title: Returning Json from Anonymous Type
body: <p>I am trying to return JSONified anonymous type from a WCF Service. </p>
  
  <p>I have been successful in doing so, but I am searching for a better alternative..</p>
  
  <pre><code>// In IService
  [OperationContract]
  [FaultContract(typeof(ProcessExecutionFault))]
  [Description("Return All Room Types")]
  [WebInvoke(UriTemplate = "/GetAllRoomTypes", Method = "GET", RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
  Stream GetAllRoomTypes();
  
  // In Service Implementation
  
  [LogBeforeAfter]
  Stream GetAllRoomTypes()
  {
     try
     {
         var allRoomTypes = Helper.GetAllRoomTypes();
         var stream = new MemoryStream();
         var writer = new StreamWriter(stream);
         writer.Write(allRoomTypes);
         writer.Flush();
         stream.Position = 0;
         return stream;
      }
      catch (Exception ex)
      {
         TableLogger.InsertExceptionMessage(ex);
         return null;
      }
  }
  
  // In Business Logic:
  
  public string GetAllRoomTypes(){
      try
      {
         return CustomRetryPolicy.GetRetryPolicy().ExecuteAction(() =&gt;
           {
            using (var context = new DatabaseEntity())
            {
               var retResult = from v in context.RoomMasters select new { Id = v.RoomTypeID, Type = v.RoomType };
               var retResult1 = retResult.ToJson();
               return retResult1;
             }
            }
           );
        }
        catch (Exception ex)
        {
          Trace.Write(String.Format("Exception Occured, Message: {0}, Stack Trace :{1} ", ex.Message, ex.StackTrace));
          return null;
        }
   }
  </code></pre>
  
  <p>My question is, is there a better way to do this?</p>