Created
April 25, 2012 16:47
-
-
Save tsbala/2491229 to your computer and use it in GitHub Desktop.
Dynamic list to typed list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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