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 8904183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:03:45+00:00 2026-06-15T02:03:45+00:00

I have a dynamic object (it’s actually json) that I pass into my MVC

  • 0

I have a dynamic object (it’s actually json) that I pass into my MVC WebApi controller.

The json object contains multiple lists within an anonymous object that are submitted to the controller from another application via client.PostAsJsonAsync("myapiurl", objectGraph).

What I need to do to validate the object on the MVC side, is to get the count of objects in each list. I can access the lists dynamically via mydynamicobject.mylist and individual items via mydynamicobject.mylist[index] but I can’t seem to be able to get a count of mydynamicobject.mylist.

What I’ve tried so far:

  • LINQ extension methods – doesn’t work on dynamic
  • Enumerable.Count(mydynamicobject.mylist) – can’t infer type

Any other ideas? The count is actually correctly available in the dynamic object’s base but obviously not accessible as a property. Help!

This works now:

// This is a MVC/WebApi method
public dynamic Post(dynamic mydynamicobject)

if (((ICollection)mydynamicobject.mylist).Count == 0)
{
// do something
}

The code that sends the dynamic object (different app):

HttpClient client = new HttpClient();  
client.DefaultRequestHeaders.Accept.Add
  (new MediaTypeWithQualityHeaderValue("application/json")); 

var objectGraph = new { mylist = new { Id = 1 }, mylist2 = new { Name = "ABC" } }; 
var r = client.PostAsJsonAsync("api/mycontroller", objectGraph).Result;
  • 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-06-15T02:03:46+00:00Added an answer on June 15, 2026 at 2:03 am

    If they are arrays, I believe you’re looking for their Length property.

    mydynamicobject.mylist.Length
    

    Alternatively, I think you might be able to get away with casting mydynamicobject.mylist to an IEnumerable and then hand it to IEnueramble.Count like so:

    IEnumerable.Count((IEnumerable)mydynamicobject.mylist);
    

    you could also do as Paolo mentioned:

    ((ICollection)mydynamicobject.mylist).Count
    

    Although I can’t take credit for that one.

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

Sidebar

Related Questions

I have dynamic json result and i want to create an object for that
I have an anonymous type object that I receive as a dynamic from a
I have JSON string that has nested objects with dynamic names that vary each
I have the following code which creates a dynamic object that is assigned to
I have a dynamic object (C# 4.0), In that i want to set an
I have the follow method that returns a dynamic object representing an IEnumerable<'a> ('a=anonymous
I have multiple dynamic objects that (most of the time) differ in only a
Before I get flagged for duplicate, I have the code from Dynamic json object
I have a number of data access methods that accept a dynamic object parameter
I have a dynamic object that sometimes is an object and sometimes is an

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.