ใช้เมธอด Select และ Lambda Expression เพื่อคำนวณเลขยกกำลังสามของสมาชิก
นี่คือลิสต์ของเรา:
List<int> list = new List<int> { 2, 4, 5, 7 };
ตอนนี้ ให้ใช้เมธอด Select() และคำนวณเลขยกกำลังสาม:
list.AsQueryable().Select(c => c * c * c);
นี่คือตัวอย่างทั้งหมด
ตัวอย่าง
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
foreach (int n in list)
Console.WriteLine(n);
// เลขยกกำลังสามของแต่ละสมาชิก
IEnumerable<int> res = list.AsQueryable().Select(c => c * c * c);
Console.WriteLine("Cube of each element...");
foreach (int n in res)
Console.WriteLine(n);
}
}
ผลลัพธ์
Elements...
2
4
5
7
Cube of each element...
8
64
125
343