.NET from-where-select用法

时间:2024-03-02 18:28:08

直接上示例代码进行说明:

1. from-where-select的用法:

int[] arr = {3, 4, 5, 6, 7, 8, 9};

int[] res = (from a in arr
                where a > 4 && a < 9
                select a).ToArray();

List<int> lst = new List<int>(res);
foreach (var item in lst) => Console.WriteLine($"item");

2. 运行结果:

5
6
7
8

3. 以上from-where-select用法可以for用法解读:

for (int i = 0; i < res.Length; i++)
{
    if (res[i] > 4 && res[i] < 9)
    {
        //TODO
    }
}