2012-01-25 63 views
0

如果您具備以下條件:如何鑄造成一個列表從一個LINQ lambda表達式

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

在Program.cs中,

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

你怎麼投的EmployeeList的進人的名單。我想這會看起來像:

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

回答

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> 
+0

非常感謝你 - 我試圖鑄造/ ToList()之前,但沒有完全得到語法正確。你的'是現貨。 – user532104