AsQueryable() in C#

The AsQueryable() method in C# is used to convert an IEnumerable collection into an IQueryable interface. This conversion enables LINQ query providers to translate queries into optimized database queries or other queryable data sources.

The key difference is that IEnumerable executes queries in-memory using LINQ to Objects, while IQueryable can be translated into expression trees for remote execution, such as database queries.

Syntax

Following is the syntax for using AsQueryable() method −

IQueryable<T> queryable = collection.AsQueryable();

The method returns an IQueryable<T> that represents the original collection as a queryable data source.

Using AsQueryable() for Sum Operation

Let us see an example to find sum of integer values using AsQueryable() method −

using System;
using System.Linq;

class Demo {
    static void Main() {
        var arr = new int[] { 100, 200, 300, 400 };
        int res = Queryable.Sum(arr.AsQueryable());
        Console.WriteLine("Sum: " + res);
    }
}

The output of the above code is −

Sum: 1000

Using AsQueryable() with Complex Queries

The AsQueryable() method becomes more useful with complex LINQ operations −

using System;
using System.Linq;

class Student {
    public string Name { get; set; }
    public int Age { get; set; }
    public int Grade { get; set; }
}

class Program {
    static void Main() {
        var students = new Student[] {
            new Student { Name = "Alice", Age = 20, Grade = 85 },
            new Student { Name = "Bob", Age = 22, Grade = 92 },
            new Student { Name = "Charlie", Age = 19, Grade = 78 }
        };

        var query = students.AsQueryable()
            .Where(s => s.Age > 19)
            .OrderByDescending(s => s.Grade)
            .Select(s => new { s.Name, s.Grade });

        Console.WriteLine("Students above 19 years, ordered by grade:");
        foreach (var student in query) {
            Console.WriteLine($"{student.Name}: {student.Grade}");
        }
    }
}

The output of the above code is −

Students above 19 years, ordered by grade:
Bob: 92
Alice: 85

IEnumerable vs IQueryable

IEnumerable IQueryable
Executes queries in-memory Can translate queries to external sources
Uses delegates (Func<T>) Uses expression trees (Expression<Func<T>>)
LINQ to Objects LINQ to SQL, Entity Framework, etc.
Suitable for in-memory collections Suitable for database queries

When to Use AsQueryable()

Use AsQueryable() when you need to −

  • Work with LINQ providers that require IQueryable interface

  • Build dynamic queries using expression trees

  • Prepare collections for potential database query translation

  • Use methods from Queryable class instead of Enumerable class

Conclusion

The AsQueryable() method converts IEnumerable collections to IQueryable, enabling expression tree-based queries that can be translated by LINQ providers. This is essential for database queries and building dynamic, composable query expressions in applications using Entity Framework or similar technologies.

Updated on: 2026-03-17T07:04:35+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements