Loading

Microsoft .Net 6 LINQ

What is IEnumerable and IQueryable in C# ?. The Complete Microsoft .Net 6 LINQ Developer Course 2022 [Videos].

In this Video, I am going to discuss IEnumerable and IQueryable in C# with examples. Please watch our previous Video where we discussed the different ways to write LINQ queries with examples. In this Video, we are going to discuss the following concepts in detail.

  1. What is IEnumerable in C#?
  2. What is IQueryable in C#?
  3. Examples of IEnumerable and IQueryable

In our previous Video, we write the following program using LINQ Query Syntax.

IEnumerable and IQueryable in C#

In the above example, we use the var keyword to create the variable and store the result of the LINQ query. So lets check what is the type of the variable? In order to check this, just mouseover the pointer on to the QuerySynntax variable and you will see that the type is IEnumerable<int> which is a generic type. So it is important to understand what is IEnumerable?

So in the above example, instead of writing the var keyword, you can also write IEnumerable<int> and it should work as expected as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> integerList = new List<int>()
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
};
IEnumerable<int> QuerySyntax = from obj in integerList
where obj > 5
select obj;
foreach (var item in QuerySyntax)
{
Console.Write(item + " ");
}
Console.ReadKey();
}
}
}

With this keep in mind, let us understand what is IEnumerable?

What is IEnumerable in C#?

Let us first see the definition of the IEnumerable which is given below.

What is IEnumerable in C#?

As we see from the above image, IEnumerable is an interface that is available in System.Collection namespace. The IEnumerable interface is a type of iteration design pattern. It means we can iterate on the collection of the type IEnumerable. As you can see in the above definition, the IEnumerable interface has one method called GetEnumerator which will return an IEnumerator that iterates through a collection.

The IEnumerable interface has also a child generic interface i.e. IEnumerable<T>. Let see the definition of IEnumerable<T> interface.

Generic IEnumerable Interface

The most important point that you need to remember is, in C#, all the collection classes (both generic and non-generic) implements the IEnumerable interface. Let us prove this by visiting the definition of List<T> generic collection class as shown in the below image.

List Definition in C#

Now let us see the definition of ArrayList collection which is a non-generic collection class.

ArrayList Collection class Definition

Note: The IEnumerable or IEnumerable<T> interface should be used only for in-memory data objects. Why that we will discuss in our next Video.

Let us see an example to understand C# IEnumerable with complex type.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> studentList = new List<Student>()
{
new Student(){ID = 1, Name = "James", Gender = "Male"},
new Student(){ID = 2, Name = "Sara", Gender = "Female"},
new Student(){ID = 3, Name = "Steve", Gender = "Male"},
new Student(){ID = 4, Name = "Pam", Gender = "Female"}
};
//Linq Query to Fetch all students with Gender Male
IEnumerable<Student> QuerySyntax = from std in studentList
where std.Gender == "Male"
select std;
//Iterate through the collection
foreach (var student in QuerySyntax)
{
Console.WriteLine( $"ID : {student.ID} Name : {student.Name}");
}
Console.ReadKey();
}
}
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}

When we execute the program it will display the result as expected as shown in the below image.

C# IEnumerable examples

What is IQueryable in C#?

Let us first see the definition of IQueryable as shown below.

What is IQueryable in C#?

As you can see in the above image, the IQueryable is an interface and it is available in System.Linq namespace. The IQuerable interface is a child of the IEnumerable interface. So we can store IQuerable in a variable of type IEnumerable. The IQuerable interface has a property called Provider which is of type IQueryProvider interface. Let us see the definition of IQueryProvider.

C# IQueryable Example

The methods provided by the IQueryProvider are used to create all Linq Providers. So, this is the best choice for other data providers such as Linq to SQL, Linq to Entities, etc. Why that we will discuss in our next Video.

Let us see an example to understand C# IQueryable interface.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> studentList = new List<Student>()
{
new Student(){ID = 1, Name = "James", Gender = "Male"},
new Student(){ID = 2, Name = "Sara", Gender = "Female"},
new Student(){ID = 3, Name = "Steve", Gender = "Male"},
new Student(){ID = 4, Name = "Pam", Gender = "Female"}
};
//Linq Query to Fetch all students with Gender Male
IQueryable<Student> MethodSyntax = studentList.AsQueryable()
.Where(std => std.Gender == "Male");
//Iterate through the collection
foreach (var student in MethodSyntax)
{
Console.WriteLine( $"ID : {student.ID} Name : {student.Name}");
}
Console.ReadKey();
}
}
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
}

Now run the application and you will see the data as expected. The point that you need to remember is in order to return a collection of type IQueryable, first, you need to call the AsQueryable() method on the collection as we did in the above example.

See All

Comments (454 Comments)

Submit Your Comment

See All Posts

Related Posts

Microsoft .Net 6 LINQ / Blog

What is Microsoft .Net 6 LINQ?

LINQ stands for Language-Integrated Query and it is a powerful query language that was introduced with .Net 3.5 & Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc.
14-Feb-2022 /38 /454

Microsoft .Net 6 LINQ / Blog

What is Architecture of LINQ?

In this article, I am going to discuss the Architecture of LINQ. The term LINQ stands for Language Integrated Query and it is pronounced as LINK. Nowadays the use of use LINQ increasing rapidly. So, as a developer, you should understand the Linq and its architecture. At the end of this article, you will have a very good understanding of the following pointers.
14-Feb-2022 /38 /454

Microsoft .Net 6 LINQ / Blog

How to write Different Ways to Write LINQ Query?

In this article, I am going to discuss the Different Ways to write LINQ Query i.e. Linq Query Syntax and Linq Method Syntax with examples. Please read our previous article where we discussed the Architecture of LINQ i.e. how LINQ works. In this article, we are going to discuss the following pointers.
14-Feb-2022 /38 /454