Let us Learn Tuple-Part7 (Last but not the least part)

Posted by in C# category on for Beginner level | Points: 250 | Views : 1745

This is the seventh and the last part of the series - Let us Learn Tuple. In this part we will look into so of the left outs that we may need sometime while dealing with Tuples.

Introduction

This is the seventh and the last part of the series - Let us Learn Tuple. In this part we will look into the left outs that we may need sometime while dealing with Tuples.

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)

(A)Sorting in Tuple?

Sorting a Tuple can be done easily by using the OrderBy or OrderByDescending extension method

Let us create a StudentsTuple as under

```List<Tuple<string, int, List<string>>> studentTupleInstances = new List<Tuple<string, int, List<string>>>();

List<string>>(
"Rajlakshmi Biswas"
, 4
, new List<string> { "Mathematics","English",
"Science and Social Science",
"Hindi"
}));
List<string>>(
, 36
, new List<string> { "Mathematics","English",
"Computer Science",
"History","Physics"
}));

List<string>>(
"Arina Biswas"
, 26
, new List<string> { "Mathematics","English",
"Accountancy",
"Finance"
}));
```

e.g to sort the above StudentsTuple by age in Descending order

```studentTupleInstances
.OrderByDescending(o=>o.Item2)
.ToList()
.ForEach
(
student =>

{
Console.WriteLine("Student Name :  " + student.Item1);
Console.WriteLine("Student Age :  " + student.Item2);

student.Item3
.ForEach
(
subject => Console.WriteLine("Is reading : " + subject)
);

Console.WriteLine("------------------------------------------");

}

);
```

In the above program we are sorting the tuple collection by age field in descending order

(B)Named items in Tuple

We have already observed that, since Tuple is a generic type henceforth, it returns the items in the form of Item1, Item2... which is very difficult to read. Let us consider the below code

```var unNamedTuple = Tuple.Create("RNA Team", 4);
var firstItem = unNamedTuple.Item1;
var secondItem = unNamedTuple.Item2;
```

There is no direct way to bring a named item in Tuple. However, one way to do so is to take the help of Anonymous Types as shown below

``` //create an AnonymousType and assign the Tuple properties to it
var namedTupleWithAnonymousType = new { Name = unNamedTuple.Item1, Age = unNamedTuple.Item2 };
Console.WriteLine(\$"Name : {namedTupleWithAnonymousType.Name} and Age: {namedTupleWithAnonymousType.Age}");
```

Another way to do so is with the help of ExpandoObject

```dynamic namedTupleWithExpandoObject = new ExpandoObject(); //create an instance of ExpandoObject class
namedTupleWithExpandoObject.Name = unNamedTuple.Item1; //assign the first tuple property to the Name property of ExpandoObject property
namedTupleWithExpandoObject.Age = unNamedTuple.Item2;//assign the second tuple property to the Age property of ExpandoObject property
Console.WriteLine(\$"Name : {namedTupleWithExpandoObject.Name} and Age: {namedTupleWithExpandoObject.Age}");
```

Another way could be to create an enum and use that as a key to the dictionary object or any name-value collection pair. Below is an example

```//create an instance of dictionary object
Dictionary<SomeEnum, dynamic> namedTupleWithEnumDictionary = new Dictionary<SomeEnum, dynamic>();

//Access the dictionary object
Console.WriteLine(\$"Name : {namedTupleWithEnumDictionary[SomeEnum.Name]} and Age: {namedTupleWithEnumDictionary[SomeEnum.Age]}");
```

Also we can create our own class and map the Tuple Items to the class properties. But all the methods discussed above are only work around.

Conclusion

So, that concludes our series Let us Learn Tuple. Hope this was useful through out. Thanks for reading. Zipped file is attached.

Recommendation

Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team

Login to vote for this post.