Link Search Menu Expand Document

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

In this video, we’ll be going to learn how we can generate custom value-tuple objects by combining two collection objects using LINQ’s zip extension method.

In this session, we’ll be going to learn how we can generate custom Value Tuple objects by combining two collection objects. If you are not aware of this Value Tuple, then let me tell you in brief about it. Value Tuple is a structure introduced in #C 7.0, which represents the value type tuple. It is already included in the dot net framework 4.7 or higher version. Basically, it allows you to store a dataset that contains multiple values that may or may not be related to each other. Now, in this example here I am returning a collection of Value Tuple type which contains a patient detail. In this lambda function (var patientDiseaseList = patinetList.Zip(diseaseHistory, (detail,disease) => ValueTuple.Create(detail.LastName, detail.FirstName, detail.ChartNo, disease)).ToList();.

Programming Essentials C

I am parsing two parameters. The first parameter is the patient detail object. The second parameter is disease history. Then in this create function of Value Tuple type, I am generating four items and it’s patient, last name, first name,  chart number, and disease. Then we get our Value Tuple list in this variable and then I’m printing the tuple items in the console

(patientDiseaseList.ForEach(tuple => Console.WriteLine($”{tuple.Item1} {tuple.Item2} had a ‘{tuple.Item4}’, few days ago.”)));

Now let’s run the application and see our result. But before that, let me add the breakpoint over here. Let’s check our list in this variable. As you can see that we have got our list of Value Tuple objects and each tuple containing patient information. Now let me click on this continue and then we get our expected data in this console here.

C Programming Essentials

This is how the Zip extension method allows us to combine two collection objects into one new custom collection. In this case, it’s a list of Value Tuple types. To learn more interesting concepts like this, stay tuned with us.

For more videos and articles visit: