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

Time:03-01

Im just starting to learn how to use LinQ, I want to only show grades that are 9 and 10 from a list i have but when trying to use var and foreach it wont let me, not sure why.

This is the list i was provided:

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

And here im trying to do the query:

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

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

The thing is that it shows an error when doing s.grades and cal.grades. I cant change the int[]grades.

CodePudding user response:

The following works:

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 is an int so you can't qualify it. You are selected an int element of an array of ints

CodePudding user response:

try this

 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);

}
  • Related