Introduction to the Language Integrated Query

When using a loop (while, do...while, or for) or enumerating (using foreach) a list, you get the value or a range of values. 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 a 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. The ability to explore a list to get or retrieve one or more values is referred to as querying.

Introduction to the Language INtegrated Query

To query a list, you can use the Language Integrated Query, abbreviated LINQ.

Creating a Query

To create a query, you write a statement using words and operators of the LINQ. The most fundamental operation you can perform on the LINQ consists of creating, also referred to as selecting, or querying, a list of values, from an existing list. The list can be an array of numbers. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>States Statistics</title>
</head>
<body>
<h2>States Statistics</h2>

@{
    int[] admissions = new int[] { 22, 49, 25, 48, 31, 38, 5, 1, 27, 4 };
}
</body>
</html>

The basic formula to query a list is:

var variable-name = from value-holder in original-list select value-holder;

The var keyword, the assignment operator "=", the from keyword, the in keyword, the select keyword, and the semicolon are required.

The variable-name is a name of a variable that will hold the list of values produced by this operation.

The value-holder is the name of a variable that will be used to identify each resulting member of this operation.

The original-list represents the name of the variable that you would have declared already. The original-list can be an array of numbers. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>States Statistics</title>
</head>
<body>
<h2>States Statistics</h2>

@{
    int[] admissions = new int[] { 22, 49, 25, 48, 31, 38, 5, 1, 27, 4 };

    var order = from n in admissions select n;

    <ul>
        @foreach(var number in order)
        {
            <li>Order of Admission to Union: @number</li>
        }
    </ul>
}
</body>
</html>

This would produce:

Introducing the Link Integrated Query - LINQ

To make the code easier to read, you can spread the select statement to various lines. Here is an example:

var order = from n in admissions select n;

This can also be done as follows:

var order = from n in admissions
            select n;

or

var order = from n
            in admissions
            select n;

One of the reasons you create a query is that you can use it in different sections of your code. As a result, you can create a query statement in one section and access it in another section.

The items of the list to query can be floating-point numbers, time values, date and time combinations, characters, etc. The rule is that the values must be of the same type or compatible.


Home Copyright © 2008-2019, FunctionX, Inc. Next