fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class EmployeeDto
  6. {
  7. public int Id { get; set; }
  8. public string Name { get; set; }
  9. }
  10.  
  11. public class EmployeeEntity
  12. {
  13. public int Id { get; set; }
  14. public string FullName { get; set; }
  15. }
  16.  
  17. class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. List<object> o = new List<object>();
  22. o.Add(new EmployeeDto { Id = 1, Name = "Bob" });
  23. o.Add(null);
  24. o.Add(new EmployeeDto { Id = 2, Name = "Foo" });
  25. o.Add(null);
  26. o.Add(null);
  27.  
  28. var employeeEntityList = MapList1(o);
  29. var employeeEntityList2 = MapList2(o);
  30.  
  31. PrintEmployee(employeeEntityList);
  32. Console.WriteLine("--");
  33. PrintEmployee(employeeEntityList2);
  34.  
  35. Console.ReadKey();
  36. }
  37.  
  38. private static void PrintEmployee(IEnumerable<EmployeeEntity> employeeEntityList)
  39. {
  40. foreach (var item in employeeEntityList)
  41. {
  42. Console.WriteLine(item.FullName);
  43. }
  44. }
  45.  
  46. private static IEnumerable<EmployeeEntity> MapList1(List<object> o)
  47. {
  48. return
  49. from i in o.OfType<EmployeeDto>()
  50. select new EmployeeEntity
  51. {
  52. Id = i.Id,
  53. FullName = i.Name
  54. };
  55. }
  56.  
  57. private static IEnumerable<EmployeeEntity> MapList2(List<object> o)
  58. {
  59. return
  60. from si in o
  61. let i = si as EmployeeDto
  62. where i != null
  63. select new EmployeeEntity
  64. {
  65. Id = i.Id,
  66. FullName = i.Name
  67. };
  68. }
  69. }
  70.  
Success #stdin #stdout 0.04s 38008KB
stdin
Standard input is empty
stdout
Bob
Foo
--
Bob
Foo