How To Convert Dynamic To List<dynamic> In C#


In this post, we are going to provide possible solutions for How To Convert Dynamic To List<dynamic> In C#, you can try below approach, to convert a dynamic variable to a list of dynamic objects (List) in C#, explained with code examples:
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // Assume we have a dynamic variable containing some data
        dynamic dynamicVariable = GetDynamicData();

        // We want to convert this dynamic variable to a list of dynamic objects
        List<dynamic> dynamicList = ConvertToDynamicList(dynamicVariable);

        // Now, we can use the dynamicList for further processing
        foreach (var item in dynamicList)
        {
            // Example: Accessing dynamic properties
            Console.WriteLine($"Dynamic property: {item.PropertyName}");
        }
    }

    // Example method to retrieve dynamic data
    static dynamic GetDynamicData()
    {
        // Assume we fetch dynamic data from somewhere
        dynamic data = new List<dynamic>
        {
            new { PropertyName = "Value 1" },
            new { PropertyName = "Value 2" },
            new { PropertyName = "Value 3" }
        };

        return data;
    }

    // Method to convert dynamic variable to List<dynamic>
    static List<dynamic> ConvertToDynamicList(dynamic dynamicData)
    {
        // Simply cast the dynamic variable to List<dynamic>
        return (List<dynamic>)dynamicData;
    }
}

We start by assuming we have some dynamic data stored in a dynamic variable (dynamicVariable) and we want to convert this dynamic variable to a list of dynamic objects (List<dynamic>).

We have defined a function ConvertToDynamicList that takes a dynamic parameter and returns a List<dynamic> and inside this method, we simply cast the dynamic variable to List<dynamic>. In the Main method, we shows the usage by retrieving dynamic data, converting it to a dynamic list, and then iterating over the list to access dynamic properties.

let's understand the example to use a Customer model with properties Id, CustomerName, EmailId, and CreatedAt. 
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // Assume we have dynamic data containing customer information
        dynamic dynamicData = GetDynamicCustomerData();

        // We want to convert this dynamic data to a list of dynamic objects
        List<dynamic> dynamicList = ConvertToDynamicList(dynamicData);

        // Now, we can use the dynamicList for further processing
        foreach (var customer in dynamicList)
        {
            // Example: Accessing dynamic properties
            Console.WriteLine($"Customer ID: {customer.Id}");
            Console.WriteLine($"Customer Name: {customer.CustomerName}");
            Console.WriteLine($"Email ID: {customer.EmailId}");
            Console.WriteLine($"Created At: {customer.CreatedAt}");
            Console.WriteLine();
        }
    }

    // Example method to retrieve dynamic customer data
    static dynamic GetDynamicCustomerData()
    {
        // Assume we fetch dynamic customer data from somewhere
        dynamic data = new List<dynamic>
        {
            new { Id = 1, CustomerName = "John Doe", EmailId = "[email protected]", CreatedAt = DateTime.Now },
            new { Id = 2, CustomerName = "Jane Smith", EmailId = "[email protected]", CreatedAt = DateTime.Now.AddDays(-1) },
            new { Id = 3, CustomerName = "Alice Johnson", EmailId = "[email protected]", CreatedAt = DateTime.Now.AddDays(-2) }
        };

        return data;
    }

    // Method to convert dynamic variable to List<dynamic>
    static List<dynamic> ConvertToDynamicList(dynamic dynamicData)
    {
        // Simply cast the dynamic variable to List<dynamic>
        return (List<dynamic>)dynamicData;
    }

    // Customer model class
    public class Customer
    {
        public int Id { get; set; }
        public string CustomerName { get; set; }
        public string EmailId { get; set; }
        public DateTime CreatedAt { get; set; }
    }
}


In this example ,we define a Customer model class with properties Id, CustomerName, EmailId, and CreatedAt and then we fetch dynamic customer data using the GetDynamicCustomerData method, which returns a dynamic list containing customer information.

and then we are converting the dynamic data to a list of dynamic objects using the ConvertToDynamicList method and we iterate over the dynamic list and access the customer properties dynamically to print in console.