Logical Conjunctions and Disjunctions
Logical Conjunctions and Disjunctions
Fundamentals of Conjunctions
Introduction
If you have created a condition that is true and you want to check whether another condition is true, you can combine them so that the combination of those conditions would be considered true if both conditions are true. The ability to combine two true conditions is called a conjunction.
To support the conjunction, the LINQ uses the && operator, which is the same as the C# language. Include the expression in a where statement. The formula to follow is:
where condition_1 && condition_2
Each condition (condition_1 and condition_2) is formulated as a Boolean expression. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from n
in numbers
where n % 2 == 0 && n <= 2000
select n;
}
<h2>Numbers</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
Once again, remember that you can (shouold) use parentheses to make your expression easier to read. Here is an example:
<!DOCTYPE html> <html> <head> <title>Basic Algebra</title> </head> <body> @{ var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466, 1248, 525, 2448, 39, 632, 2077 }; var number = from n in numbers where (n % 2 == 0) && (n <= 2000) select n; } <h2>Basic Algebra</h2> <ul> @foreach (var member in number) { <li>@member</li> } </ul> </body> </html>
Or better yet:
<!DOCTYPE html> <html> <head> <title>Basic Algebra</title> </head> <body> @{ var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466, 1248, 525, 2448, 39, 632, 2077 }; var number = from n in numbers where ((n % 2) == 0) && (n <= 2000) select n; } <h2>Basic Algebra</h2> <ul> @foreach (var member in number) { <li>@member</li> } </ul> </body> </html>
Arranging the Results of a Conjunction
After getting a list of numbers produced by a conjunction, you may want to arrange the list in incrementing order. To do this, just before the select clause, type orderby followed by the variable of the from section. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from nbr
in numbers
where (nbr % 2 == 0) && (nbr <= 2000)
orderby nbr
select nbr;
}
<h2>Numbers</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
As you can see, the list is arranged in ascending order. If you want to reinforce this, you can add the ascending keyword at the end of the orderby clause. Here is an example:
var number = from nbr
in numbers
where (nbr % 2 == 0) && (nbr <= 2000)
orderby nbr ascending
select nbr;
If you want to order the list in reverse, use the descending keyword. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from nbr
in numbers
where (nbr % 2 == 0) && (nbr <= 2000)
orderby nbr descending
select nbr;
}
<h2>Numbers</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
Negating a Condition
We already know that, to negate a condition, you can precede it with the ! operator. Here is an example of negating one condition in a conjunction:
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from n
in numbers
where !(n % 2 == 0) && (n <= 2000)
select n;
}
<h2>Basic Algebra</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
Of course, you can negate either of the conditions. Here is an example that negates the second condition:
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from n
in numbers
where (n % 2 == 0) && !(n <= 2000)
select n;
}
<h2>Basic Algebra</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
Combining Conjunctions
You can combine as many conditions as you want. If you want the whole combination to be true only if all conditions are true, create the expression as a conjunction. In this case, separate the conditions with the && operator. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Exercise</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from n
in numbers
where ((n % 2) == 0) && (n >= 50) && (n <= 2000)
select n;
}
<h2>Basic Algebra</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
Negating the Conditions of a Conjunction
You can also negate both conditions by preceding each with the ! operator. Here is an example:
<!DOCTYPE html> <html> <head> <title>Exercise</title> </head> <body> @{ var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466, 1248, 525, 2448, 39, 632, 2077 }; var number = from n in numbers where !((n % 2) == 0) && !(n <= 2000) select n; } <h2>Basic Algebra</h2> <ul> @foreach (var member in number) { <li>@member</li> } </ul> </body> </html>
This would produce:
Negating a Conjunction
To negate a conjunction, precede it with a ! operator. Remember that a conjunction is made of two parts. Therefore, if you want to negate the whole conjunction, put the conjunction in parentheses but precede it with ! (outside the parentheses). Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Basic Algebra</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from n
in numbers
where !(((n % 2) == 0) && (n <= 2000))
select n;
}
<h2>Basic Algebra</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
Disjunctions
Introduction
If you are checking many conditions for a list you have, you many want only one of those conditions to be true for the combination to be true. The ability to combine various conditions so that only one of them needs to be true for the whole group to be true, is called a disjunction.
To support disjunctions, the LINQ provides the || operator. This is the same operator used in the C# language. To perform a disjunction, create a where condition, followed by (at least) two conditions. Separate the conditions with the || operator. The formula to follow is:
where condition_1 || condition_2
Each condition is created as a Boolean expression. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Numbers</title>
</head>
<body>
@{
var numbers = new int[] { 82, 45, 1424, 3548, 38, 5, 466,
1248, 525, 2448, 39, 632, 2077 };
var number = from n
in numbers
where n <= 50 || n >= 2000
select n;
}
<h2>Numbers</h2>
<ul>
@foreach (var member in number)
{
<li>@member</li>
}
</ul>
</body>
</html>
This would produce:
To make a disjunction easier to read, you should put each condition in its own parentheses. Here is an example:
var number = from n in numbers where (n <= 50) || (n >= 2000) select n;
Combining Disjunctions
As seen with the conjunction, you can combine various conditions but you may want only one of those conditions to be true. In this case, in the where clause, create as many conditions as you want but separate them with the || operator.
|
||
Previous | Copyright © 2010-2019, FunctionX, Inc. | Next |
|