Melo Tang Melo Tang - 3 months ago 34
ASP.NET (C#) Question

Lambda select many expression

I have a EmployeeTest as below and I want to "print each employee information" by lambda.
I got System.Collections.Generic.List`1[System.String]

How can I fix my lambda expression?

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/>");
}


output messages:

ANS1:Tom,ASP.NET

ANS1:Tom,MVC

ANS1:Tom,C#

ANS1:Mike,ASP.NET

ANS1:Mike,MVC

ANS1:Mike,SQL

ANS2:1,System.Collections.Generic.List
1[System.String],12345678<
ANS2:1,System.Collections.Generic.List
1[System.String],999999999

ANS2:2,System.Collections.Generic.List
1[System.String],7777777
ANS2:2,System.Collections.Generic.List
1[System.String],999999999

Answer
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 -- need sleep