title: Pass dynamic data type to function
body: <p>I have the following function:</p>
  
  <pre><code>private string ParseJson(dynamic q)
      {
          string returnJSON = "[{ \"type\" : \"pie\", \"name\" : \"Campaigns\", \"data\" : [ ";
          foreach (var grp in q)
          {
              double currCount = grp.Count();
              if (grp.Key != null)
                  returnJSON += "['" + grp.Key + "', " + currCount + "],";
              else
                  returnJSON += "['none', " + currCount + "],";
          }
          returnJSON = returnJSON.Substring(0, returnJSON.Length - 1);
          returnJSON += "]}]";
  
          return returnJSON;
      }
  </code></pre>
  
  <p>I call it from methods like this one:</p>
  
  <pre><code>public string GetCampaignData()
      {
          PaymentModelDataContext db = new PaymentModelDataContext();
          var q = from Event in db.TrackingEvents
                  group Event by Event.campaignID;
  
  
          return ParseJson(q);
  
      }
  </code></pre>
  
  <p>I use q for several different queries, all grouping data.</p>
  
  <p>The problem is that the runtime can't bind a type to q for some reason. Is this a proper use of dynamic? Is there a different way to do this?</p>