Skip to content

Instantly share code, notes, and snippets.

@tsbala
Created April 25, 2012 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsbala/2491229 to your computer and use it in GitHub Desktop.
Save tsbala/2491229 to your computer and use it in GitHub Desktop.
Dynamic list to typed list
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
public class IdName
{
public int Id { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
var source = new List<dynamic>
{
new {Id = 1, Name = "Test 1", Value = 100},
new {Id = 2, Name = "Test 2", Value = 200 },
new {Id = 3, Name = "Test 3", Value = 300 },
new {Id = 4, Name = "Test 4", Value = 400 },
};
var idNames = new List<IdName>();
source.ForEach(o => idNames.Add(AutoMapper.Mapper.DynamicMap<IdName>(o)));
foreach (var idName in idNames)
{
Console.WriteLine("{0} - {1}", idName.Id, idName.Name);
}
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment