How to Loop Through List in C# Using Linq

Introduction

We are blessed with a bunch of collections to use in C#. And the collection is no good if we can't iterate through them.

Well, C# has these 3 interfaces to do the work. Each one of them functions specifically with their qualities.

Before we understand their meaning. We first need to know their structure in C# 9.

Following UML will give you an understanding of their place in C# and which namespaces they belong to.

IEnumerable And IEnumerator In C#

Let us understand how IEnumerable works.

IEnumerable

Say we have a user-defined class person having properties such as Name, Height, Weight & Sex.

  1. public class  Person
  2.    {
  3. public string  Name { get ; set ; }
  4. public string  Sex { get ; set ; }
  5. public decimal  Height { get ; set ; }
  6. public int   Weight { get ; set ; }
  7.    }

Now in our Main method first we need to initialize the list, then we will iterate it using IEnumerable with LINQ & Lambda expressions.

  • The list will have 2 males & 2 females
  • Iterate males using lambda
  • Iterate females using LINQ
  1. using  I_Cant_See_Sharp.Entities;
  2. using  System;
  3. using  System.Collections;
  4. using  System.Collections.Generic;
  5. using  System.Linq;
  6. namespace  I_Cant_See_Sharp
  7. {
  8. class  Program
  9.     {
  10. static void  Main( string [] args)
  11.         {
  12.             List<Person> listOfPersons =new  List<Person>()
  13.             {
  14. new  Person()
  15.                 {
  16.                     Name ="Alex" ,
  17.                     Height = 5.5M,
  18.                     Weight = 56,
  19.                     Sex ="Male"
  20.                 },new  Person()
  21.                 {
  22.                     Name ="Martin" ,
  23.                     Height = 5.10M,
  24.                     Weight = 58,
  25.                     Sex ="Male"
  26.                 },new  Person()
  27.                 {
  28.                     Name ="Gloria" ,
  29.                     Height = 5.3M,
  30.                     Weight = 52,
  31.                     Sex ="Female"
  32.                 },new  Person()
  33.                 {
  34.                     Name ="Sophia" ,
  35.                     Height = 5.7M,
  36.                     Weight = 76,
  37.                     Sex ="Female"
  38.                 }
  39.             };
  40.             Console.WriteLine("-------------------- With Lambda Expressions  --------------------------" );
  41.             IEnumerable<Person> listOfMales =new  List<Person>();
  42.             listOfMales = listOfPersons.Where(male => male.Sex =="Male" );
  43. foreach  (var item in  listOfMales)
  44.             {
  45.              Console.WriteLine(item.Name);
  46.             }
  47.             Console.WriteLine(Environment.NewLine);
  48.             Console.WriteLine("---------------------------- With LINQ  --------------------------------" );
  49.             IEnumerable<Person> listOfFemales =new  List<Person>();
  50.             listOfFemales = from femalein  listOfPersons
  51.                             where female.Sex =="Female"
  52.                             select female;
  53. foreach  (var item in  listOfFemales)
  54.             {
  55.                 Console.WriteLine(item.Name);
  56.             }
  57.         }
  58.     }
  59. }

Now let's check out the output:

IEnumerable And IEnumerator In C#

IEnumerator

Let's divide our list based on Weight.

Weight < 60 & weight > 60

The list has a GetEnumator function, as we have also seen this in UML above. This function returns an IEnumerator of generics

  • MoveNext()
    It jumps on the next record: Helps to traverse the list. basically it does position++
  • Reset()
    Reset the list which then points back to the -1 index. It makes  position = -1
  • Current
    Returns the current object.
  • Advantage
    It remembers the current state. How?
    • Let's understand this with an example, we will print all persons with age less than 60 and then pass the same list to new function, here new function will start printing the list from same index where it was left rather than starting from the 1st index again.

With above listOfPerson we can do as follows to get an IEnumarator

  1. IEnumerator<Person> listOfMales = listOfPersons.GetEnumerator();

 The following code is a complete example of IEnumarator:

  1. class  Program
  2.   {
  3. static void  Main( string [] args)
  4.       {
  5.           List<Person> listOfPersons =new  List<Person>()
  6.           {
  7. new  Person()
  8.               {
  9.                   Name ="Alex" ,
  10.                   Height = 5.5M,
  11.                   Weight = 56,
  12.                   Sex ="Male"
  13.               },new  Person()
  14.               {
  15.                   Name ="Martin" ,
  16.                   Height = 5.10M,
  17.                   Weight = 56,
  18.                   Sex ="Male"
  19.               },new  Person()
  20.               {
  21.                   Name ="Gloria" ,
  22.                   Height = 5.3M,
  23.                   Weight = 52,
  24.                   Sex ="Female"
  25.               },new  Person()
  26.               {
  27.                   Name ="Sophia" ,
  28.                   Height = 5.7M,
  29.                   Weight = 76,
  30.                   Sex ="Female"
  31.               }
  32.           };
  33.           Console.WriteLine("-------------------- Less than 60  --------------------------" );
  34.           IEnumerator<Person> listOfLessSixty= listOfPersons.GetEnumerator();
  35. while  (listOfLessSixty.MoveNext())
  36.           {
  37. if (listOfLessSixty.Current.Weight > 60)
  38.               {
  39.                   Console.WriteLine(Environment.NewLine);
  40.                   Console.WriteLine("------------------- Greater than 60  -------------------------" );
  41.                   DisplayFemales(listOfLessSixty);
  42.               }
  43. else
  44.               {
  45.               Console.WriteLine("Name: " +listOfLessSixty.Current.Name + "Age: "  + listOfLessSixty.Current.Weight);
  46.               }
  47.           }
  48.       }
  49. static void  DisplayFemales(IEnumerator<Person> listOfGreaterSixty)
  50.       {
  51.               Console.WriteLine("Name: "  + listOfGreaterSixty.Current.Name + "Age: "  + listOfGreaterSixty.Current.Weight);
  52.       }
  53.   }

Perfect! Let's see an output:

IEnumerable And IEnumerator In C#

If we had to pass IEnumerable instead of IEnumarator, we will end up printing the list twice.

  1. using  I_Cant_See_Sharp.Entities;
  2. using  System;
  3. using  System.Collections;
  4. using  System.Collections.Generic;
  5. using  System.Linq;
  6. namespace  I_Cant_See_Sharp
  7. {
  8. class  Program
  9.     {
  10. static void  Main( string [] args)
  11.         {
  12.             List<Person> listOfPersons =new  List<Person>()
  13.             {
  14. new  Person()
  15.                 {
  16.                     Name ="Alex" ,
  17.                     Height = 5.5M,
  18.                     Weight = 56,
  19.                     Sex ="Male"
  20.                 },new  Person()
  21.                 {
  22.                     Name ="Martin" ,
  23.                     Height = 5.10M,
  24.                     Weight = 56,
  25.                     Sex ="Male"
  26.                 },new  Person()
  27.                 {
  28.                     Name ="Gloria" ,
  29.                     Height = 5.3M,
  30.                     Weight = 52,
  31.                     Sex ="Female"
  32.                 },new  Person()
  33.                 {
  34.                     Name ="Sophia" ,
  35.                     Height = 5.7M,
  36.                     Weight = 76,
  37.                     Sex ="Female"
  38.                 }
  39.             };
  40.             IEnumerable<Person> listOfPeople = listOfPersons;
  41. foreach  (var item in  listOfPeople)
  42.             {
  43.                 Console.WriteLine("Name: "  + item.Name + " Age: "  + item.Weight);
  44. if (item.Weight > 60)
  45.                 {
  46.                     DisplayFemales(listOfPeople);
  47.                 }
  48.             }
  49.         }
  50. static void  DisplayFemales(IEnumerable<Person> listOfPeople)
  51.         {
  52. foreach  (var item in  listOfPeople)
  53.             {
  54.                 Console.WriteLine("Name: "  + item.Name + " Age: "  + item.Weight);
  55.             }
  56.         }
  57.     }
  58. }

As per code, we are now passing IEnumerable. Let's check out the output:

IEnumerable And IEnumerator In C#

As per output, it is clear that IEnumerable does not remember the current state it was processing. Thus it starts processing back from the 1st index.

Conclusion

In this article, we learned how to and when to use IEnumerable & IEnumerator.

What are the properties & methods of these interfaces we can use?

In the next article, we will learn how everything about IQueryable.

I hope you now have a basic understanding of these iterators. Use them in your C# code as per the project's requirement.

If you want to say hello, connect with me @

  • Twitter
  • LinkedIn
  • GitHub

How to Loop Through List in C# Using Linq

Source: https://www.c-sharpcorner.com/article/ienumerable-iqueryable-ienumerator-in-c-sharp/

0 Response to "How to Loop Through List in C# Using Linq"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel