Lambda选择了许多表达式

时间:2016-09-06 06:57:27

标签: c# asp.net lambda

我有一个EmployeeTest,如下所示,我想用lambda“打印每个员工信息”。
我得到了System.Collections.Generic.List1[System.String]
我该如何修复我的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:Tom,ASP.NET
ANS1:Tom,MVC
ANS1:Tom,C#
  ANS1:Mike,ASP.NET
ANS1:Mike,MVC
ANS1:Mike,SQL
  ANS2:1,System.Collections.Generic.List1 [System.String],12345678&LT;   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个

1 个答案:

答案 0 :(得分:0)

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

然后......

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