• C#联接序列


      1.  Concat() – 串联序列(有重复项)

     var healthFoods = new List<string> { "fruits", "vegetables", "grains", "proteins" };
    
     var myFoods = new List<string> { "grains", "proteins", "M&Ms", "soda" };
      
     var healthyFirst = healthFoods.Concat(myFoods); // 返回序列: fruits, vegetables, grains, proteins, grains, proteins, M&Ms, soda
    
    
    var mineFirst = myFoods.Concat(healthFoods);// 返回序列: grains, proteins, M&Ms, soda, fruits, vegetables, grains, proteins

      2. Union() – 串联序列(无重复项)

     var healthyFirst = healthFoods.Union(myFoods);// 返回序列: fruits, vegetables, grains, proteins, M&Ms, soda
    
     var mineFirst = myFoods.Union(healthFoods);// 返回序列: grains, proteins, M&Ms, soda, fruits, vegetables

    3.Zip() – 简单一对一的联接注:Employee 和Seat是两个对象

    var employees = new List<Employee>
    
        {
    
             new Employee { Id = 13, Name = "John Doe", Salary = 13482.50 },
             new Employee { Id = 42, Name = "Sue Smith", Salary = 98234.13 },
             new Employee { Id = 99, Name = "Jane Doe", Salary = 32421.12 }
    
         };
    
     var seats = new List<Seat>
         {
             new Seat { Id = 1, Cost = 42 },
             new Seat { Id = 2, Cost = 42 },
             new Seat { Id = 3, Cost = 100 },
             new Seat { Id = 4, Cost = 100 },
             new Seat { Id = 5, Cost = 125 },
             new Seat { Id = 6, Cost = 125 },
         };

    //接下来使用Zip()
    var seatingAssignments = employees.Zip(seats, (e, s) => new { EmployeeId = e.Id, SeatId = s.Id });
    foreach (var seat in seatingAssignments) { Console.WriteLine("雇员: " + seat.EmployeeId + " 预约了座位 " + seat.SeatId);
    }

    //运行结果(注:一旦到达较短序列的最后一项,它就会立即停止)

         雇员: 13 预约了座位1

         雇员: 42 预约了座位2

         雇员: 99 预约了座位3

    4.Join() – 满足条件的联接(注:EmployeeBadge是两个对象)

    var employees = new List<Employee>
         {
             new Employee { Id = 13, Name = "John Doe", Salary = 13482.50 },
             new Employee { Id = 42, Name = "Sue Smith", Salary = 98234.13 },
             new Employee { Id = 99, Name = "Jane Doe", Salary = 32421.12 }
         };
     var badges = new List<Badge>
         {
             new Badge { EmployeeId = 10, BadgeNumber = 1 },
             new Badge { EmployeeId = 13, BadgeNumber = 2 },
             new Badge { EmployeeId = 20, BadgeNumber = 3 },
             new Badge { EmployeeId = 25, BadgeNumber = 4 },
             new Badge { EmployeeId = 42, BadgeNumber = 5 },
             new Badge { EmployeeId = 10, BadgeNumber = 6 },
             new Badge { EmployeeId = 13, BadgeNumber = 7 },
         };
    
    //接下来使用Join()
    var badgeAssignments = employees.Join(badges, e => e.Id, b => b.EmployeeId,  (e, b) => new { e.Name, b.BadgeNumber });
    
     foreach (var badge in badgeAssignments)
     {
    Console.WriteLine("
    Name: " + badge.Name + " has badge " + badge.BadgeNumber);
    }


    返回结果:(注:不会返回Jane Doe的信息)
    Name: John Doe has badge 2 Name: John Doe has badge 7 Name: Sue Smith has badge 5

    5.GroupJoin() – 适用于一对多的条件联接

    var badgeAssignments = employees.GroupJoin(b adges, e => e.Id, b => b.EmployeeId, (e, bList) => new { Name = e.Name, Badges = bList.ToList() });

    foreach (var assignment in badgeAssignments)
    {
    Console.WriteLine(assignment.Name + " has badges:");

    if (assignment.Badges.Count > 0)
    {
             foreach (var badge in assignment.Badges)
             {
    Console.WriteLine(" Badge: " + badge.BadgeNumber);
    }

    }
    else {

    Console.WriteLine(" No badges.");

    }

    }


    //输出结果
    John Doe has badges:
    
             Badge: 2
    
             Badge: 7
    
     Sue Smith has badges:
    
             Badge: 5
    
     Jane Doe has badges:
    
             No badges.
  • 相关阅读:
    poj 3273 Monthly Expense(贪心+二分)
    codeforces 235 div2 C Team
    ZOJ 3607 Lazier Salesgirl(贪心)
    poj 1185 炮兵阵地(三维状态压缩dP)
    poj 2411 Mondriaan's Dream(状态压缩dP)
    sdut 2819 比赛排名(边表 拓扑排序)
    hdu 1421 搬寝室(dp)
    hdu 1243 反恐训练营(dp 最大公共子序列变形)
    Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
    hdu 1559 最大子矩阵 (简单dp)
  • 原文地址:https://www.cnblogs.com/weimingxin/p/8109633.html
Copyright © 2020-2023  润新知