Querying a List
The Language Integrated Query
Introduction to Querying
When using for or foreach loops on a list, you get the value or a range of values inside of the
loop. Once you exit the loop, the operation ends and you cannot access the value(s) that was
(were) isolated. If you want to get the isolated value or an isolated list of values again, you
would have to perform the operation (create the loop), again. In some cases, you may want to
prepare and get one value, a few values, or a range of values for later use, or to use over and
over again. To do this, you would create a value or a list of values and store that list in a
variable, outside of any loop, then use the value or the list of values when needed. As applied
to the for or the foreach loop, to perform this operation, you use a conditional statement that
would examine the list, look for the value(s), get that value or those values that respond(s) to
the condition. Any value(s) that respond(s) to the condition is(are) then stored in the new list.
This technique of examining an array is referred to as querying.
To support the ability to query a list, you can use the Language Integrated Query, abbreviated
LINQ. To use LINQ in your application, you must include the System.Core.dll assembly in your
program. If you started your application as an empty project:
● On the main menu, you can click Project -> Add Reference...
● In the Solution Explorer, you can right-click the name of the project and click Add Reference...
● In the Class View, you can right-click the name of the project and click Add Reference...
In the .NET tab of the Add Reference dialog box, you can click System.Core
Then click OK. You must then use the System.Linq namespace in your code or you should
include the using System.Linq; line in your list of namespaces.
If you create an application by selecting the Console Application option from the New Project
dialog box, the studio would add the necessary assembly to your project and the necessary