Link Search Menu Expand Document

How to Merge 2 Collection Objects using LINQ's Zip Method - Part 1

The Zip is an extension method that merges the corresponding elements of two collection objects using a specified selector function. In this video, we’ll be going to learn about how we can combine two collection objects and then iterate over them by generating one logical collection using LINQ’s zip extension method.

In #C, can we combine two collection objects and then generate custom objects from them? If yes, then what is the best way? In this session, we’ll be going to learn about how we can combine two collection objects. Then iterate over it by generating one logical collection using LINQ’s Zip method. If you are from a python background, then Zip may be familiar to you. The Zip is an extension method in the LINQ’s namespace, which allows us to join two collection objects together. Here is the basic declaration of the Zip extension method. This (IEnumberable<TSecond> second) extension method takes the second collection object and pairs each element in the two collections together based on a position. This (Func<Tfirst, Tsecond, Tresult> resultSelector) function will perform the actual merging.

Now without going much into the theory, let’s see, Zip method in action. Here in this code, our first collection object consists of a patient name and we will be going to merge the object with the patient disease history. For each parameter of an item in the sequences, we will execute this [private static string CombineObjectData(string patientName, object oldDisease)] combined object data function and return the resulting data to the patient disease list object. Then over here I have explicitly called To List methods to force evolution. So that we can see the combined list in use.

Programming Essentials C#

Now let’s run this demo. Here we can see that our combined data object method is called and in the parameter for the first time, you can see the John Doe as patient name, which is the first item over here. Then in the disease name, you can see the ‘Asthma’, which is the first item in this disease history object. Now, click on the continue button. For the second object, we will get the patient’s name as Anna Smith, which is over here. Disease name as a skin infection, which is the second item in this disease history object. Same way for the third item, which is Peter Jones as a patient.

Now, put one more breakpoint over here, and let’s click on CONTINUE. Check the final result in this object. We have got the three strings as expected. Then we will write these three string objects into the console using the ‘For Each’ method. Let’s click on Continue and you can see our combined object as output in the console. This is how we effectively Zip two collection objects into one logical object. What happens if these two collection objects have a different length? I mean, let’s say if I add one more patient name over here. But I have not added the fourth item in the disease history object.

Programming Essentials C Sharp

Run the app again and see what happens. As you can see that we have got the same output as before, but not that we don’t have a Peter Parker in this list. That is because the Zip method ran out of disease objects after it got Peter Jones. It means that the Zip extension method stops when it runs out of items in one of the two sequences.

For more videos and articles visit: