准备:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public double Salary { get; set; }
}
public class Seat
{
public int Id { get; set; }
public double Cost { get; set; }
}
public class Badge
{
public int EmployeeId { get; set; }
public int BadgeNumber { get; set; }
}
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 },
};
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 },
};
var healthFoods = new List<string> { "fruits", "vegetables", "grains", "proteins" };
var myFoods = new List<string> { "grains", "proteins", "M&Ms", "soda" };
//1. Concat() – 串联序列 最简单的序列合并,concat仅仅是将第二个序列接在第一个序列后面, 注意:返回的序列并没有改变原来元素的顺序
var healthyFirst1 = healthFoods.Concat(myFoods); // 返回序列: fruits, vegetables, grains, proteins, grains, proteins, M&Ms, soda
var mineFirst1 = myFoods.Concat(healthFoods);// 返回序列: grains, proteins, M&Ms, soda, fruits, vegetables, grains, proteins
//2. Union() – 无重复项的串联序列(去重)
//该方法用于结合两个没有重复项的集合,也非常适用于任何两个序列。 它将第二个序列结合到第一个序列里,
//当第二个序列中出现与第一个序列重复的项时,它只保留第一序列的项目。
var healthyFirst2 = healthFoods.Union(myFoods); // 返回序列: fruits, vegetables, grains, proteins, M&Ms, soda
var mineFirst2 = myFoods.Union(healthFoods);// 返回序列: grains, proteins, M&Ms, soda, fruits, vegetables
//3.Zip() – 简单一对一的联接
//该方法针对两个不同类执行一个最简单的联接。 比如给定两个序列,它仅仅将他们的第一个项合并,
//而后将他们第二个项合并,…,一旦到达较短序列的最后一项,它就会立即停止。
var seatingAssignments = employees.Zip(seats, (e, s) => new { EmployeeId = e.Id, SeatId = s.Id, e.Name, e.Salary });
foreach (var seat in seatingAssignments)
{
Console.WriteLine("雇员: " + seat.EmployeeId + " 预约了座位 " + seat.SeatId + " " + seat.Salary);
}
//只有3笔数据,是因为employees只有3笔
//4. Join() – 满足条件的联接
var badgeAssignments = employees.Join(badges, e => e.Id, b => b.EmployeeId, (e, b) => new { e.Name, e.Salary, b.BadgeNumber });
foreach (var badge in badgeAssignments)
{
Console.WriteLine("Name: " + badge.Name + " has badge " + badge.BadgeNumber + " " + badge.Salary);
}
//Id要和EmployeeId相等
//5.GroupJoin() – 适用于一对多的条件联接
//那么,如果你有1:N的关系,你希望这些结果分类组合在一起就可以用到 GroupJoin()
var badgeAssignments1 = employees.GroupJoin(badges, e => e.Id, b => b.EmployeeId,
(e, bList) => new { Name = e.Name, Badges = bList.ToList() });
foreach (var assignment in badgeAssignments1)
{
Console.WriteLine(assignment.Name + " has badges:");
if (assignment.Badges.Count > 0)
{
foreach (var badge in assignment.Badges)
{
Console.WriteLine("\tBadge: " + badge.BadgeNumber);
}
}
else
{
Console.WriteLine("\tNo badges.");
}
}
标签:EmployeeId,合并,---,var,序列,字符串,new,Id,Name From: https://www.cnblogs.com/apple-hu/p/18230934