Introduction to Criteria

Where is the Condition

Remember that you can use the LINQ to query a list. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 45, 525, 2448, 39, 45, 632, 207 };

    var values = from n
                 in numbers
                 select n;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

A criterion is a condition applied to a list of values to find out which one(s) respond(s) to the condition. When applied to a list, a criterion examines each member, finds out what member responds to it, if so, adds that member to the from list.

To apply a criterion, create a Boolean expression between the in and the select statements. This criterion is formulated using the where operator. The formula to follow is:

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

In the new section, the where keyword is required. When creating a conditional statement in a query, the condition is formulated using one of the logical operators from the C# language.

Comparison for Equality

To get a list of values that match a certain value of your choice, you can use the == operator. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 45, 525, 2448, 39, 45, 632, 207, 45 };

    var values = from nbr in numbers
                 where nbr == 45
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

A Lower Value

To get a list of values lower than a certain one, use the Less Than operator <. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 38, 5, 128, 45, 525, 2448, 39, 632, 45, 207 };

    var values = from nbr in numbers
                 where nbr < 45
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

A Lower or Equal Value

To get a list of values that are lower or equal to a certain value, you can use the Less Than Or Equal operator <=. Here is an example:

<!DOCTYPE html>

<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 38, 5, 128, 45, 525, 2448, 39, 632, 45, 207 };

    var values = from nbr in numbers
                 where nbr <= 45
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

A Greater Value

To get a list of values that are greater than a certain value, you can use the Greater Than operator >. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 38, 5, 128, 45, 525, 2448, 39, 632, 45, 207 };

    var values = from nbr in numbers
                 where nbr > 45
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

A Greater or Equal Value

To get a list of values that are greater or equal to a certain value, you can use the Greater Than Or Equal operator >=. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 38, 5, 128, 45, 525, 2448, 39, 632, 45, 207 };

    var values = from nbr in numbers
                 where nbr >= 45
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

A Different Value

To get a list of values that are different from a certain value, you can use the != operator. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 45, 525, 2448, 39, 45, 632, 207, 45 };

    var values = from nbr in numbers
                 where nbr != 45
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Introduction to Criteria

Other Operations on a Query

Negating a Condition

You can create a criterion that works perfectly but rather want its opposite. To get it, you can negate the expression. To do this, use the ! operator (of the C# language) before the criterion. You must include the Boolean expression in parentheses and precede it with the ! operator. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 45, 525, 2448, 39, 45, 632, 207, 45 };

    var values = from nbr in numbers
                 where !(nbr <= 122)
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
    <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Numbers

Arithmetic Operators

In the first part of a conditional statement, you can use an arithmetic operation. For example, you can add two numbers or find the remainder of a number with regards to another such as "number % 5";. Then, in the second part, you can perform a logical operation. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Basic Algebra</title>
</head>
<body>
@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 525, 2448, 39, 632, 207 };


    var number = from n
                 in numbers
                 where n % 5 == 0
                 select n;
}

<h2>Basic Algebra</h2>

<ul>
    @foreach (var member in number)
    {
        <li>@member</li>
    }
</ul>
</body>
</html>

This would produce:

To make the statement easier to read and less confusing, you should make it a habit to isolate the groups of statements in parentheses. Here is an example:

@{
    var number = from n
                 in numbers
                 where (n % 5) == 0
                 select n;
}

Sorting a Conditional List

Sorting in Asccending Order

When getting a list of values from a query, if you want, you can arrange it in an order of your choice. If you have a list of numbers, to arrange it in incrementing order, apply the orderby clause before the select statement. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 45, 525, 2448, 39, 45, 632, 207, 45 };

    var values = from nbr in numbers
                 where nbr != 45
                 orderby nbr
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
        <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Arranging a Queried List

As an option, you can end the orderby clause with the ascending keyword.

Sorting in Desccending Order

Ir you want to arrange a queried list in reverse incremting order, before the select statement, type the orderby keyword, the from variable, and the descending keyword. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
<h1>Numbers</h1>

@{
    var numbers = new int[] { 12, 45, 38, 5, 128, 45, 525, 2448, 39, 45, 632, 207, 45 };

    var values = from nbr in numbers
                 where nbr != 45
                 orderby nbr descending
                 select nbr;
}

<ul>
    @foreach (var nbr in values)
    {
        <li>@nbr</li>
    }
</ul>
</body>
</html>

This would produce:

Arranging a Queried List


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