title: C# How To return List<> As Json
body: <p>I am having problem with Json and Lists</p>
  
  <p>I am trying to return a list of Chat Entity class but when i try to return it the compiler whines.
  I also tried to return IEnumerable&lt;> but the same error came</p>
  
  <p>what can i do ?</p>
  
  <p>here is my function that is returning the item,</p>
  
  <pre><code>public List&lt;Chat&gt; GetNewChatPosts()
  { 
      var userID = U_rep.GetUserID(User.Identity.Name);
      var list = G_rep.GetNewestChat(0, userID);
      return Json(list);
  }
  </code></pre>
  
  <p>This is the Get Newest Chat function</p>
  
  <pre><code>public List&lt;Chat&gt; GetNewestChat(int gameID, int userID)
  { 
      var pos1 = (from p in n_db.ChatPos
                  where p.userID == userID &amp;&amp; gameID == p.gameID
                  select p).SingleOrDefault();
      int pos;
      if (pos1 == null)
      {
          pos = 0;
          ChatPo n = new ChatPo();
          n.gameID = gameID;
          n.userID = userID;
          n.chatID = pos;
  
          n_db.ChatPos.InsertOnSubmit(n);
          n_db.SubmitChanges();
      }
      else
      {
          pos = pos1.ID;
      }
  
      var newIEnumerable = from chat in n_db.Chats
                              where chat.ID &gt; pos
                              orderby chat.ID descending
                              select chat;
      List&lt;Chat&gt; newestChat = new List&lt;Chat&gt;();
      foreach (var n in newIEnumerable)
      {
          newestChat.Add(n);
      }
  
      var last = newIEnumerable.Last();
  
      pos1.ID = last.ID;
  
      n_db.SubmitChanges();
  
      return newestChat;    
  }
  </code></pre>
  
  <p>and This is the Ajax call</p>
  
  <pre><code>$.ajax({
      type: "GET",
      url: "/Game/GetNewChatPosts",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: JSON.stringify(text),
      success: function (data) {
          alert("success post");
      },
      error: function () { alert("error post"); }
  });
  </code></pre>