Sử dụng phương pháp Chọn và Biểu thức Lambda để tính khối lập phương của các phần tử.
Sau đây là danh sách của chúng tôi.
List<int> list = new List<int> { 2, 4, 5, 7 }; Bây giờ, hãy sử dụng phương thức Select () và tính toán khối lập phương.
list.AsQueryable().Select(c => c * c * c);
Sau đây là toàn bộ ví dụ.
Ví dụ
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> list = new List<int> { 2, 4, 5, 7 };
Console.WriteLine("Elements...");
// initial list javascript:void(0)
foreach (int n in list)
Console.WriteLine(n);
// cube of each element
IEnumerable<int> res = list.AsQueryable().Select(c => c * c * c);
Console.WriteLine("Cube of each element...");
foreach (int n in res)
Console.WriteLine(n);
}
} Đầu ra
Elements... 2 4 5 7 Cube of each element... 8 64 125 343