ใช้เมธอด AsQueryable() เพื่อรับการอ้างอิง IQueryable
เรามาดูตัวอย่างเพื่อหาผลรวมของค่าจำนวนเต็ม
ขั้นแรก ตั้งค่าอาร์เรย์จำนวนเต็ม
var arr = new int[] { 100, 200, 300, 400 }; ตอนนี้เพื่อค้นหาผลรวม ใช้เมธอด Queryable Sum() และ AsQueryable()
Queryable.Sum(arr.AsQueryable());
ต่อไปนี้เป็นรหัสที่สมบูรณ์
ตัวอย่าง
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);
}
} ผลลัพธ์
Sum: 1000