using System;
using System.Collections.Generic;
using System.Linq;
public class EmployeeDto
{
public int Id { get; set; }
public string Name { get; set; }
}
public class EmployeeEntity
{
public int Id { get; set; }
public string FullName { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<object> o = new List<object>();
o.Add(new EmployeeDto { Id = 1, Name = "Bob" });
o.Add(null);
o.Add(new EmployeeDto { Id = 2, Name = "Foo" });
o.Add(null);
o.Add(null);
var employeeEntityList = MapList1(o);
var employeeEntityList2 = MapList2(o);
PrintEmployee(employeeEntityList);
Console.WriteLine("--");
PrintEmployee(employeeEntityList2);
Console.ReadKey();
}
private static void PrintEmployee(IEnumerable<EmployeeEntity> employeeEntityList)
{
foreach (var item in employeeEntityList)
{
Console.WriteLine(item.FullName);
}
}
private static IEnumerable<EmployeeEntity> MapList1(List<object> o)
{
return
from i in o.OfType<EmployeeDto>()
select new EmployeeEntity
{
Id = i.Id,
FullName = i.Name
};
}
private static IEnumerable<EmployeeEntity> MapList2(List<object> o)
{
return
from si in o
let i = si as EmployeeDto
where i != null
select new EmployeeEntity
{
Id = i.Id,
FullName = i.Name
};
}
}