Difference between IEnumerable,IQuerable and IEnumerator

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

We use Ienumerable and Iquerable with collection to get the benefits of Linq

Difference Between IEnumerable and IQuerable :

  • IEnumerable and IQuerable both are support deferred execution.
  • If you use where query in Linq than In case of IEnumerable, IT will get all the data in memory and than apply where query to filter the result whereas is case of IQuerable,  it apply the where query at the database level and get only the required result in memory.
    So this way IQuerable perform fast as compare to IEnumerable.

 

Any element in a collection can be retrieved through its index property. But instead of element index, the IEnumerator provides two abstract methods and a property to pull a particular element in a collection. And they are Reset()MoveNext() and Current.

See the figure. This is how it works.

Difference Between IEnumerable and IEnumerator :

  • IEnumerable provide the function GetEnumerator which will give you the Enumerator as result.
    Example:

    1
    2
    3
    4
    5
    6
    7
    
    List<int> list = new List<int>();
        list.Add(1);
        list.Add(5);
        list.Add(9);
     
        List<int>.Enumerator e = list.GetEnumerator();
        Write(e);
    List<int> list = new List<int>();
    	list.Add(1);
    	list.Add(5);
    	list.Add(9);
    
    	List<int>.Enumerator e = list.GetEnumerator();
    	Write(e);
  • Ienumerator provide you two extra function “Reset()”, “MoveNext()” and “Current”
    Example:

    1
    2
    3
    4
    
        if (list.MoveNext())
        {
        Console.WriteLine(list.Current.ToString());
        }
        if (list.MoveNext())
        {
        Console.WriteLine(list.Current.ToString());
        }

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

2 thoughts on “Difference between IEnumerable,IQuerable and IEnumerator

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.