C# Querying With LINQ Now You're Querying Writing Your First LINQ Query

Getting compiler error CS0201.

using System.Linq; List<int> numbersGreaterThanTen = new List<int>(); foreach (int number in numbers) { if (number > 10) { numbersGreaterThanTen.Add(number); } }

numbersGreaterThanTen List<int>(3) { 16, 32, 64 } from number in numbers where number > 10 select number; (1,1): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Steven Parker
Steven Parker
173,876 Points

To properly display posted code, always use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

But a much better way to share code from a workspace is to use the "snapshot" function and post the link it gives you.

Evan O'Hara
Evan O'Hara
7,063 Points

The reason this is happening probably has to do with how their workspace is setup to simplify some things. I got this in Visual Studio by declaring numbersGreaterThanTen as an IEnumerable<int>.

So working in Visual Studio is gonna be something like this:

List<int> numbers = new List<int> { 2, 4, 8, 16, 32, 64 }; IEnumerable<int> numbersGreaterThanTen = new List<int>(); numbersGreaterThanTen = from number in numbers where number > 10 select number;