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

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 695 red flag

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.


 Download source code for Let us Learn Tuple-Part7 (Last but not the least part)

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>>>();

//Add the first record
studentTupleInstances.Add(new Tuple<string, int, 
List<string>>(
                        "Rajlakshmi Biswas"
                        , 4
                        , new List<string> { "Mathematics","English",
                                            "Science and Social Science",
                                             "Hindi"
                                          }));
//Add the second record
studentTupleInstances.Add(new Tuple<string, int, 
List<string>>(
                        "Niladri Biswas"
                        , 36
                        , new List<string> { "Mathematics","English",
                                            "Computer Science",
                                             "History","Physics"
                                          }));

//Add the third record
studentTupleInstances.Add(new Tuple<string, int, 
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>();

//Add tuples items to it
namedTupleWithEnumDictionary.Add(SomeEnum.Name, unNamedTuple.Item1);
namedTupleWithEnumDictionary.Add(SomeEnum.Age, unNamedTuple.Item2);

//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
Read Let us Learn Tuple-Part1 after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

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


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)