AsEnumerable() ép kiểu (cast) một kiểu cụ thể thành IEnumerable<T> tương đương. Là extension method trong System.Linq.
Ví dụ minh họa
using System;
using System.Linq;
class Demo
{
static void Main()
{
int[] arr = new int[5];
arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50;
var res = arr.AsEnumerable();
foreach (var ele in res)
Console.WriteLine(ele);
}
}
Kết quả đầu ra
10 20 30 40 50
Ứng dụng chính: Buộc dùng LINQ to Objects (in-memory) thay vì LINQ provider (EF, SQL). Ví dụ: dbContext.Table.AsEnumerable().Where(x => ComplexMethod(x)) - tránh lỗi "cannot be translated to SQL".