LINQ AsQueryable Method

In LINQ, the AsQueryable operator/method is useful for converting input list elements to IQueryable<T> list, and AsQueryable is a method of System.Linq.Queryable class.

Syntax of LINQ AsQueryable Method

Following is the syntax of using the LINQ AsQueryable method to convert list elements to IQueryable lists. 

LINQ AsQueryable Syntax in C#

IQueryable<Student> query = objStudent.AsQueryable().Where(student => student.Name.Contains("Alavala"));

LINQ AsQueryable Syntax in VB.NET

Dim query As IQueryable(Of Student) = objStudent.AsQueryable().Where(Function(student) student.Name.Contains("Alavala"))

If you observe the above syntax, we converted the “objStudent” list to IQueryable to get the required data based on conditions.

Example of LINQ AsQueryable Method

Following is the example of using the LINQ AsQueryable method to convert input list items to IQueryable.

 

C# Code

 

using System;
using System.Linq;
using System.Collections.Generic;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Student> objStudent = new List<Student>()
      {
        new Student() { Name = "Suresh Dasari", Gender = "Male",Location="Chennai" },
        new Student() { Name = "Rohini Alavala", Gender = "Female", Location="Chennai" },
        new Student() { Name = "Praveen Alavala", Gender = "Male",Location="Bangalore" },
        new Student() { Name = "Sateesh Alavala", Gender = "Male", Location ="Vizag"},
        new Student() { Name = "Madhav Sai", Gender = "Male", Location="Nagpur"}
      };
      IQueryable<Student> query = objStudent.AsQueryable().Where(student => student.Name.Contains("Alavala"));
      foreach (var student in query)
      {
         Console.WriteLine(student.Name);
      }
      Console.ReadLine();
    }
  }
  class Student
  {
    public string Name { get; set; }
    public string Gender { get; set; }
    public string Location { get; set; }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim objStudent As New List(Of Student)() From {
New Student() With {.Name = "Suresh Dasari", .Gender = "Male", .Location = "Chennai"},
New Student() With {.Name = "Rohini Alavala", .Gender = "Female", .Location = "Chennai"},
New Student() With {.Name = "Praveen Alavala", .Gender = "Male", .Location = "Bangalore"},
New Student() With {.Name = "Sateesh Alavala", .Gender = "Male", .Location = "Vizag"},
New Student() With {.Name = "Madhav Sai", .Gender = "Male", .Location = "Nagpur"}
}
Dim query As IQueryable(Of Student) = objStudent.AsQueryable().Where(Function(student) student.Name.Contains("Alavala"))
For Each student In query
Console.WriteLine(student.Name)
Next
Console.ReadLine()
End Sub

Class Student
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set(ByVal value As String)
m_Gender = value
End Set
End Property
Private m_Gender As String
Public Property Location() As String
Get
Return m_Location
End Get
Set(ByVal value As String)
m_Location = value
End Set
End Property
Private m_Location As String
End Class
End Module

The above example shows that we are converting the “objStudent” list/collection items to IQueryable to filter data using the AsQueryable method.

Result of LINQ AsQueryable Method Example

Following is the result of the LINQ AsQueryable method example.

 

Rohini Alavala
Praveen Alavala
Sateesh Alavala

This is how we can use the LINQ AsQueryable method to convert input sequence elements to an IQueryable list to get required elements by filtering the list in c# / vb.net.