LINQ AsQueryable Method

  Total Views : 325
  Comments : 0
 
  Keywords : Linq asqueryable operator with example in c# / vb.net, Use linq asqueryable method to convert list elements to iqueryable with example in c# / vb.net, Linq queryable.asqueryable method

Here we will learn linq asqueryable method in c# / vb.net with example and how to use linq asqueryable method to convert list elements to iqueryable list with example in c# / vb.net.

LINQ AsQueryable Method

In LINQ AsQueryable operator / method is used to convert given 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 LINQ AsQueryable method to convert list elements to IQueryable list.

 

C# Code

 

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

VB.NET Code

 

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

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

Example of LINQ AsQueryable Method

Following is the example of using 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

If you observe above example we are converting “objStudent” list / collection items to IQueryable to filter data using AsQueryable method.

Output of LINQ AsQueryable Method Example

Following is the result of LINQ AsQueryable method example.

 

Rohini Alavala

Praveen Alavala

Sateesh Alavala

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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User