2016-09-06 63 views
-1

我有一個EmployeeTest,如下所示,我想通過lambda「打印每個員工信息」。
我得到System.Collections.Generic.List1[System.String]
我該如何解決我的lambda表達式?Lambda選擇很多表達式

public class EmployeeTest 
    { 
     public int ID { get; set; } 
     public List<string> Skills { get; set; } 
     public List<int> PhoneNumbers { get; set; } 

     public static List<EmployeeTest> GetAllEmployees() 
     { 

      List<EmployeeTest> listEmployees = new List<EmployeeTest> 
      { 
       new EmployeeTest 
       { 
        ID = 1, 
        Skills = new List<string>{"ASP.NET","MVC","C#"}, 
        PhoneNumbers = new List<int>{12345678,999999999} 
       }, 
       new EmployeeTest 
       { 
        ID = 2, 
        Skills = new List<string>{"ASP.NET","MVC","SQL"}, 
        PhoneNumbers = new List<int>{7777777,999999999} 
       } 
      }; 

      return listEmployees; 

     } 
    } 

var result5 = EmployeeTest.GetAllEmployees().SelectMany(e2 => e2.PhoneNumbers, (Employee1, PhoneNumbers) => new {EmployeeID = Employee1.ID,EmployeeSkills=Employee1.Skills.ToString(),EmployeeNum = PhoneNumbers }); 

foreach (var V in result5) 
      { 
       Response.Write("ANS2:" + V.EmployeeID + "," + V.EmployeeSkills + "," + V.EmployeeNum + "<br/>"); 
      } 

輸出消息:

ANS1:湯姆,ASP.NET
ANS1:湯姆,MVC
ANS1:湯姆,C#
ANS1:邁克,ASP.NET
ANS1: Mike,MVC
ANS1:Mike,SQL
ANS2:1,System.Collections.Generic.List1 [System.String],12345678 < ANS2:1,System.Collections.Generic.List1 [System.String],999999999
ANS2:2,System.Collections.Generic.List1 [System.String],7777777 ANS2:2,System.Collections.Generic.List1 [System.String],999999999

+4

你還沒說什麼* *是錯誤的 - 請澄清你的問題,最好是使用[MCVE。此外,請更多關注您的帖子佈局......源代碼之前已遍佈各地,而且它仍然*大量*滾動到右側。水平滾動使帖子非常難以閱讀。 –

+0

感謝您的建議 –

+0

請提及您遇到的問題,如錯誤信息等。 –

回答

0
var result5 = EmployeeTest.GetAllEmployees().SelectMany(e2 => e2.PhoneNumbers, (Employee1, PhoneNumbers) => new {EmployeeID = Employee1.ID,EmployeeSkills=Employee1.Skills,EmployeeNum = PhoneNumbers }); 

THEN ...

foreach (var V in result5) 
     { 
      Response.Write("ANS2:" + V.EmployeeID + "," + String.Join(",", V.EmployeeSkills) + "," + V.EmployeeNum + "<br/>"); 
     } 

Arrgh - 需要睡眠

+0

嗨香農我得到了同樣的結果。我認爲問題是我的lambda表達式。 –

+0

是 - 將ToString()更改爲String.Join(「,」,Employee1.Skills) –

+0

否 - 對不起。堅持 –