我如何使用 where 和 int[] 列表进行 LinQ 查询?

How can i make a LinQ query using where and with an int[] list?

我刚开始学习如何使用 LinQ, 我只想显示我拥有的列表中的 9 和 10 年级,但是在尝试使用 var 和 foreach 时它不会让我,不知道为什么。

这是我提供的列表:

int[] grades= { 5, 9, 7, 8, 6, 9, 5, 7, 7, 4, 6, 10, 8 };

我在这里尝试查询:

        var grades1 = from s in grades
        where s.grades>= 9 
        select s;

        foreach (var cal in grades)
        Console.WriteLine(cal.grades);

问题是它在执行 s.grades 和 cal.grades 时显示错误。我无法更改 int[]grades.

以下作品:

int[] grades = { 5, 9, 7, 8, 6, 9, 5, 7, 7, 4, 6, 10, 8 };
var grades1 = from s in grades
              where s >= 9
              select s;

foreach (var cal in grades1)
    Console.WriteLine(cal);

s 是一个 int,所以你不能限定它。您正在选择一个整数数组

int 元素

试试这个

 int[] grades = { 5, 9, 7, 8, 6, 9, 5, 7, 7, 4, 6, 10, 8 };

var grades1 = from s in grades
              where s >= 9
              select s;

foreach (var cal in grades1)
{
    Console.WriteLine(cal);

}