首页 > 代码库 > LINQ中ForEach方法的使用

LINQ中ForEach方法的使用

标签: linq
2016-06-25 12:47 409人阅读 评论(0) 收藏 举报
 分类: LINQ(5)   我の原创(113)  
版权声明:本文为博主原创文章,未经博主允许不得转载。

LINQ中ForEach方法的使用。

1、实例一:使用ForEach获取列表中的某个字段值

[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
/// <summary>  
/// 使用ForEach获取列表中员工名称字段  
/// </summary>  
static void Main(string[] args)  
{  
    List<Emplayee> emplayeeList = GetEmplayeeList();  //获取员工信息列表    
    string empNames = "";                             //员工名称字段  
    emplayeeList.ForEach(a => empNames += a.EmplayeeName + ",");  
    empNames = empNames.TrimEnd(‘,‘);  
    Console.WriteLine(empNames);                     //输出:张三,李四,王五  
}  
2、实例二:使用ForEach将部门列表与员工列表关联 

[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
/// <summary>    
/// 使用ForEach将部门列表与员工列表关联   
/// </summary>     
public static void JoinDepartmentList()  
{  
    List<Department> departmentList = GetDepartmentList();   //获取部门信息列表    
    List<Emplayee> emplayeeList = GetEmplayeeList();         //获取员工信息列表    
    departmentList.ForEach(a => a.EmplayeeList = emplayeeList.Where(e => e.DepartmentId == a.DepartmentId).ToList());  
  
    //使用ForEach输入结果  
    departmentList.ForEach(a => Console.WriteLine(String.Format("{0}:员工数量:{1}", a.DepartmentName, a.EmplayeeList.Count)));  
}  
其它代码:

[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
/// <summary>    
/// 部门信息类    
/// </summary>    
public class Department  
{  
    /// <summary>    
    /// 部门ID    
    /// </summary>    
    public int DepartmentId { get; set; }  
  
    /// <summary>    
    /// 部门名称    
    /// </summary>    
    public string DepartmentName { get; set; }  
  
    /// <summary>  
    /// 员工列表  
    /// </summary>  
    public List<Emplayee> EmplayeeList { get; set; }  
}    
  
/// <summary>    
/// 员工信息类    
/// </summary>    
public class Emplayee  
{  
    /// <summary>    
    /// 员工姓名    
    /// </summary>    
    public string EmplayeeName { get; set; }  
  
    /// <summary>    
    /// 部门ID    
    /// </summary>    
    public int DepartmentId { get; set; }  
}  
[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
/// <summary>    
/// 获取员工信息列表    
/// </summary>    
/// <returns></returns>    
public static List<Emplayee> GetEmplayeeList()  
{  
    List<Emplayee> emplayeeList = new List<Emplayee>();  
    Emplayee emplayee1 = new Emplayee() { EmplayeeName = "张三", DepartmentId = 1, };  
    Emplayee emplayee2 = new Emplayee() { EmplayeeName = "李四", DepartmentId = 2, };  
    Emplayee emplayee3 = new Emplayee() { EmplayeeName = "王五", DepartmentId = 2, };  
    emplayeeList.Add(emplayee1);  
    emplayeeList.Add(emplayee2);  
    emplayeeList.Add(emplayee3);  
    return emplayeeList;  
}  
  
/// <summary>    
/// 获取部门信息列表    
/// </summary>    
/// <returns></returns>    
public static List<Department> GetDepartmentList()  
{  
    List<Department> departmentList = new List<Department>();  
    Department department1 = new Department() { DepartmentId = 1, DepartmentName = "研发部" };  
    Department department2 = new Department() { DepartmentId = 2, DepartmentName = "人事部" };  
    Department department3 = new Department() { DepartmentId = 3, DepartmentName = "财务部" };  
    departmentList.Add(department1);  
    departmentList.Add(department2);  
    departmentList.Add(department3);  
    return departmentList;  
}  

顶
0
踩

  

LINQ中ForEach方法的使用