Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:15:14+00:00 2026-05-24T17:15:14+00:00

I have the following function: private string ParseJson(dynamic q) { string returnJSON = [{

  • 0

I have the following function:

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;
    }

I call it from methods like this one:

public string GetCampaignData()
    {
        PaymentModelDataContext db = new PaymentModelDataContext();
        var q = from Event in db.TrackingEvents
                group Event by Event.campaignID;


        return ParseJson(q);

    }

I use q for several different queries, all grouping data.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:15:16+00:00Added an answer on May 24, 2026 at 5:15 pm

    The problem is Count() is an extension method off of IEnumerable<T>, as such it can’t be called from dynamic (because it’s not a true method of the class).

    Your variable grp is also dynamic because it results from an expression on dynamic variable q:

    foreach (var grp in q)
    

    Since we can’t call extension methods off of dynamic (again, they aren’t true members of the class), we need to explicitly call the extension method instead off the Enumerable static class. So change your code to:

    double currCount = Enumerable.Count(grp);
    

    And you’ll see it work properly for the Count(), if you want to really use dynamic.

    That said, I do agree with @John’s comment that you should consider changing this to a non-dynamic. Actually, what your method would accept would be an IEnumerable> like so:

    private string ParseJson<TKey,TValue>(IEnumerable<IGrouping<TKey,TValue>> 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;
        }
    

    You can also make the parameter type non-generic specific to your usage if you like. But this would work with all groupings…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String
I have the following function private byte[] Function(string url) { HttpWebRequest webRequest= (HttpWebRequest)WebRequest.Create(url); webRequest.AddRange(0,
I have the following function: Private Function XMLEncode(ByVal s As String) As String Return
I have the following classes: type TSong = class(TObject) private FArtist: String; FTitle: String;
I have the following function: private void populate() { String connectionString = Properties.Settings.Default.Database; String
I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
If I have the following registry class: Class registry { private $_vars; public function
I have the following function that is pulling data from a database. The ajax
I have the following function: public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String
I have created the following function for checking the connection status: private void checkConnectionStatus()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.