如何从Linq lambda表达式转换为列表

时间:2012-01-25 13:46:22

标签: linq list

如果您有以下内容:

Public Class Person
{
   public int Id;
   Public String Name; 
}

在Program.cs中,

String[] employeeList = new String[]{ 3, 5, 7 } 

如何将employeeList转换为Person列表。我觉得它看起来像是:

List<Person> persons = employeeList
   .Select( e=> new { 
      Id = e.ToString(), 
      Name = e.ToString() + "hello" 
   } );

1 个答案:

答案 0 :(得分:3)

List<Person> persons = employeeList
// In the select part you just indicate the desired type to return
.Select(e => new Person { 
    Id = e.ToString(), 
    Name = e.ToString() + "hello" 
}).ToList(); // and cast the resulted enumerable to List<Person>