Document directory
Today, let's take a look at five methods that use the Linq function to join sequences. These five methods can be classified into the following two categories:
Similar connections
Connections of different classes
- Zip ()
- Join ()
- GroupJoin ()
Concat ()-series Sequence
In the simplest sequence merging, concat only connects the second sequence to the end of the first sequence. Note: The returned sequence does not change the sequence of the original elements:
Var healthFoods = new List <string> {"fruits", "vegetables", "grains", "proteins"}; var myFoods = new List <string> {"grains ", "proteins", "M & Ms", "soda"}; // return sequence: fruits, vegetables, grains, proteins, grains, proteins, M & Ms, soda var healthyFirst = healthFoods. concat (myFoods); // return sequence: grains, proteins, M & Ms, soda, fruits, vegetables, grains, proteins var mineFirst = myFoods. concat (healthFoods );
Union ()-series of non-repeated items
This method is used to combine two sets without repeated items and is very suitable for any two sequences. It combines the second sequence into the first sequence. When the second sequence contains items that are repeated with the first sequence, it retains only the items of the first sequence.
Whether the project is repeated depends onIEqualityComparer <T>If you do not provide a personalized definition, use the default function of this type. Note that ifTIs a custom type, which means you need a validEquals ()AndGetHashCode ().
// Return sequence: fruits, vegetables, grains, proteins, M & Ms, soda var healthyFirst = healthFoods. union (myFoods); // return sequence: grains, proteins, M & Ms, soda, fruits, vegetables var mineFirst = myFoods. union (healthFoods );
Zip ()-simple one-to-one join
This method executes a simple join for two different classes. For example, given two sequences, it only merges their first item, and then merges their second item ,..., Once the last item of the shorter sequence is reached, it will immediately stop.
For example, we have the following class definitions:
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; } }
Then, we confirm the following order:
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 }, };
We can connect them to provide each employee with a seat:
Var seatingAssignments = employees. zip (seats, (e, s) => new {EmployeeId = e. id, SeatId = s. id}); foreach (var seat in seatingAssignments) {Console. writeLine ("Employee:" + seat. employeeId + "reserved seats" + seat. seatId );}
We can get:
Employee: 13. Reserved seat 1
Employee: 42 reserved seats 2
Employee: 99 reserved seats 3
Join ()-Join that meets the conditions
"The join clause can be used to associate elements from different source sequences that are not directly related to the object model. The only requirement is that the elements in each source need to share a value that can be compared to determine whether it is equal. For example, a food dealer may have a list of suppliers and buyers for a product. For example, you can use the join clause to create a list of suppliers and buyers in the same region of the product.
The join clause accepts two source sequences as input. Each element in a sequence must be an attribute that can be compared with the corresponding attribute in another sequence, or contain such an attribute. The join clause uses the special equals keyword to compare whether the specified key is equal. All joins executed by the join clause are equivalent joins. The output form of the join clause depends on the type of the join executed. "
Whether it is equal depends onIEqualityComparer <T>,If you use a custom type, you need to provideEquals ()AndGetHashCode ()Or provide a customIEqualityComparer <T>.However, the type in. NET you are using does not need to implement these functions.
In the example above, we use the previous Employee class and add a Badge class, and then we create this sequence:
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 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 }, };
Then we can use Join to operate them:
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); }
The returned result is:
Name: John Doe has badge 2 Name: John Doe has badge 7 Name: Sue Smith has badge 5
Join is very useful for the relationship, or if you do not care about returning repeated 1: N relationships, you can also use Join.
GroupJoin ()-applicable to one-to-many conditional join
If you have a 1: N relationship, you can use these results in combination.GroupJoin (), Still use the following example:
var badgeAssignments = employees.GroupJoin(badges, 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("\tBadge: " + badge.BadgeNumber); } } else { Console.WriteLine("\tNo badges."); } }
The result is as follows:
John Doe has badges: Badge: 2 Badge: 7 Sue Smith has badges: Badge: 5 Jane Doe has badges: No badges.
If you want to improve your understanding of the difference between Join and GroupJoin, you can take a look at the results of the two examples above.
For more exciting articles, read ASP. NET (Alex Song)
Translated from:C #/. NET Little Wonders: 5 Easy Ways to Combine Sequences