Add Items to Dynamic List in C#.Net

Hi to add item from List to Dynamic list using c#.net, .net core. Here we will add and get the value from dynamic object and display in console.
In today's article will show a simple tutorial with example how to add a new element to list object of dynamic type in c#.net. So, for this article we will perform example in console application in .NET core using c#.net. Here we will take a list object of type dynamic add the value in it. after adding value, we will get and display the value. 

So, let's create a new console application. Now in this console application we will add a class property file. This we will use for making a class having some value and we will store or add the Employee List value in List<dynamic>. 

public class EmployeeProperty
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
} 
Now let's add some value to Employee List. 
List<EmployeeProperty> employees = new List<EmployeeProperty>()
{
    new EmployeeProperty{ Id=1,Name="Rakesh", Address="R. Address"},
    new EmployeeProperty{ Id=2,Name="Dhiraj", Address="D. Address"},
    new EmployeeProperty{ Id=3,Name="Jonhn", Address="J. Address"},
    new EmployeeProperty{ Id=4,Name="Mat", Address="M. Address"}
}; 
In above employee list I have added some value in it. Now we will see how we can add the value of employee list into List<dynamic>.
List<dynamic> dynamicList = new List<dynamic>();
foreach (var employee in employees)
{
    dynamicList.Add(new { employee.Id, employee.Name, employee.Address });
} 
In above code I have user foreach loop to read all values in Employee List and after that I added it to List<dynamic> of anonymous type.  Now let read the value from List<dynamic>. 
//Get List<dynamic> value
foreach (var item in dynamicList)
{
    int emp_Id = 0;
    string name_emp = string.Empty;
    string address_emp = string.Empty;

    var Id = item.GetType().GetProperty("Id");
    if (Id != null)
    {
        emp_Id = Id.GetValue(item);
    }
    var Name = item.GetType().GetProperty("Name");
    if (Name != null)
    {
        name_emp = Name.GetValue(item);
    }
    var Address = item.GetType().GetProperty("Address");
    if (Address != null)
    {
        address_emp = Address.GetValue(item);
    }
    Console.WriteLine("Id" + emp_Id + " ,Name: " + name_emp + " Address: " + address_emp);
}
Console.ReadLine(); 
Here in above code I have used foreach loop. Here first I have got the property by using from dynamic objet using item.GetType().GetProperty("Id"). After that I check property exists or not if it is null on that case, we will not try to get the value otherwise will "Object reference error". Now to get the value from dynamic object e Id?.GetValue(item, null);. At the end we have displayed the value. Now lets put a break pint and check the output.

Dynamic List Data

Here we can see we are having all record in dynamic list. Now let's see the value to get. 
Dynamic List item Value

Here we are able to get the value.  Now lets press F5 to check the output.

Dynamic List item Value in Console

Post a Comment