In this fifth part, we will learn how to use LAMBDA with Tuple
Introduction
This is the fifth part of the series Let us Learn Tuple. In this fifth part, we will learn how to use LAMBDA with Tuple
You can read the entire series of article as listed down
Let us Learn Tuple-Part1
Let us Learn Tuple-Part2 ( Creation of Generic Collections and Accessing the same using Tuple )
Let us Learn Tuple-Part3 ( Tuple and Arrays )
Let us Learn Tuple-Part4 ( Create MultiKey dictionary using Tuple )
Let us Learn Tuple-Part5 ( LAMBDA with Tuple )
Let us Learn Tuple-Part6 (Tuple Serialization and DeSerialization)
Let us Learn Tuple-Part7 (Last but not the least part)
Using the code
For this to explain, we will consider two entities, OriginalMobileNumber and PresentMobileNumber.Let us populate some data to those and the objective is to find out those MobileNumbers that have been changed.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
public class LinqWithTuple
{
public void LinqWithTupleExample()
{
var originalMobileNumber = new List<OriginalMobileNumber>()
{
new OriginalMobileNumber { MobileNumber = "1111111111" }
,new OriginalMobileNumber { MobileNumber = "2222222222" }
,new OriginalMobileNumber { MobileNumber = "3333333333" }
,new OriginalMobileNumber { MobileNumber = "4444444444" }
,new OriginalMobileNumber { MobileNumber = "5555555555" }
};
var presentMobileNumber = new List<PresentMobileNumber>()
{
new PresentMobileNumber { MobileNumber = "1111111111" }
,new PresentMobileNumber { MobileNumber = "2112112121" }
,new PresentMobileNumber { MobileNumber = "3333333333" }
,new PresentMobileNumber { MobileNumber = "4224114114" }
,new PresentMobileNumber { MobileNumber = "5445115211" }
};
}
}
public class OriginalMobileNumber
{
public string MobileNumber { get; set; }
}
public class PresentMobileNumber
{
public string MobileNumber { get; set; }
}
}
We have two sets viz. OriginalMobileNumber and PresentMobileNumber.First, we will find out those elements that has been changed in second set.Next we will find out those elements that has been changed in first set.Once done, we will merge the two sets results to get the desired result.
//Step 1: Values of First Set Changed In Second Set
var valuesofFirstSetChangedInSecondSet = originalMobileNumber.Select(a => a.MobileNumber).Except(presentMobileNumber.Select(c => c.MobileNumber));
//Step 2: Values of Second Set Changed In First Set
var valuesofSecondSetChangedInFirstSet = presentMobileNumber.Select(a => a.MobileNumber).Except(originalMobileNumber.Select(c => c.MobileNumber));
//Step 3: Merge the two sets results to get the desired result
valuesofFirstSetChangedInSecondSet
.Zip(
valuesofSecondSetChangedInFirstSet, (o, p) =>
new Tuple<string, string>(o,p)
)
.ToList()
.ForEach(
i => Console.WriteLine("Original Mobile# : " + i.Item1 + " has changed to : " + i.Item2) //print the result
);
The above example presented the query syntax way to use Tuple with Lambda. We are using the Except extension method of the static Enumerable class. It produces the set difference of two sequences by using the default equality comparer to compare values.Once done, we need to merge the two sets for which we are using the Zip extension method that merges two sequences by using the specified predicate function. The output is as under

Let us consider another example.
Here will will look into how to find/filter elements in Tuple using LAMBDA
Let us consider the above example. Suppose we want to find out those records where the number "4" is present. We can do that as shown under
//filter those records where the number "4" is present
valuesofFirstSetChangedInSecondSet
.Zip(
valuesofSecondSetChangedInFirstSet, (o, p) =>
new Tuple<string, string>(o, p)
)
.Where(w => w.Item2.Contains("4")) //filter the records inside the Tuple
.ToList()
.ForEach(
i => Console.WriteLine("Original Mobile# : " + i.Item1 + " has changed to : " + i.Item2) //print the result
);
So, inside the where extension method, we have passed the relevant predicate function in order to filter the records of the tuple.
The output is as under

Conclusion
In this article we learnt about using Lambda with Tuple. Hope this will be helpful. More to come in the following series. Zipped file attached.