Conditional Conjunctions

Nesting a Conditional Query

Remember that you can create a query that has a condition as follows:

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

Normally, when you are creating a conjunction, you are asking the compiler to check one condition followed by another. Another way to perform this validation, or a way to create a complex query, is to create a LINQ statement in the place of the list, which is referred to as nesting. The query you are nesting will provide the list from which the condition will apply. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 25, 380, 69, 285, 2448, 32, 6320, 4 };

var original     = from n
in numbers
orderby n
select n;

var lessThan445 = from val
in numbers
where val < 445
select val;

var values      = from nbr
in from nbr
in numbers
where nbr < 445
select nbr
where nbr > 25
select nbr;

// Whole List
foreach (var member in original)

// Numbers that are inferior to 445
foreach (var v in lessThan445)

// Numbers between 25 and 445
foreach (var w in values)
}
}
}```

This would produce: Once again, to make your code easy to read, you should put the nested query in parentheses. Also, you can sort the nested query if you want.

Introduction to Logical Conjunction

A logical conjunction consists of combining two Boolean expressions that each produces its own true or false result. In your LINQ statements, you can involve one or more conditional conjunctions. To do this, in the where statement, instead of one, create as many conditions as you want. Between each combination of two conditions, include a conjunction operator.

A Binary Logical Conjunction

As you may know already, a conjunction in C# can be performed using the & operator. The operator can be implemented in LINQ as follows:

`var sub-list-name = from value-holder in list where condition select value-holder;`

The condition is formulated as a Boolean condition. For a conjunction, the condition is in the form:

`condition-1 & condition-2`

Therefore, the formula of using a conjunction in a LINQ expression is:

```var sub-list-name = from value-holder
in list
where condition-1 & condition-2
select value-holder;```

In this formula, each condition, condition-1 or condition-2, is formulated as a separate Boolean expression.

Verifying Equality

To verify equalities in a conjunction, you need two propositions. If you are checking the values from a list of primitive values, you may need a separate variable to compare to a constant. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
string status = "Full-Time";

double[] timesWorked = new[] { 9.50, 8.00, 7.00, 8.00, 8.00,
9.00, 7.50, 8.00, 8.00, 7.50  };

var work = from val
in timesWorked
where val == 8.00 & status == "Full-Time"
select val;

foreach (var wk in work)
}
}
}```

This would produce: Sectioning a Condition

To make your code easy to use, you can include the whole conjunction statement in parentheses. This can be done as follows:

```var work = from val
in timesWorked
where (val == 8.00 & status == "Full-Time")
select val;```

To make each condition easy to use, you can include that condition in parentheses. You can put just one condition in parentheses or each in its own parentheses:

```var work = from val
in timesWorked
where (val == 8.00) & (status == "Full-Time")
select val;```

A more effective way to use a conjunction is to start from a list that contains various categories of values, which is the case for classes, records, structures, tuples, etc.

Negating a Condition

To consider the opposite of a condition, you can precede it with the ! operator. To negate a complete conjunction, write its whole statement in parentheses and precede it with the ! operator. This can be done as follows:

```var work = from val
in timesWorked
where !(val == 8.00 & status == "Full-Time")
select val;```

To negate a condition, put that condition in parentheses but precede the opening parenthesis with the ! operator. You can negate one condition or each one of them. Here are examples:

```var work = from val
in timesWorked
where !(val == 8.00) & !(status == "Full-Time")
select val;```

Verifying Inequality

When creating a query, to get a list of numbers that are not equal to a constant of your choice, use the != operator. As opposed to the == operator, the != operator makes it logically possible for two conditions to use the same or different variables. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 500, 380, 6, 1285, 2548, 1000, 6320, 4 };

var number = from n
in numbers
select n;

var selections = from n
in numbers
where n != 500 & n != 1000
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

This would produce: If you find it necessary, you can arrange the list produced by the query. To do this, apply the orderby operator before the final select clause. Here are examples:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 500, 380, 6, 1285, 2548, 1000, 6320, 4 };

var number     = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n != 500 & n != 1000
orderby n ascending
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

This would produce: Verifying Inferiority and Superiority of a Value

The C# language supports many comparison operators:

• To get a list of values that are lower than a value of your choice, use the is < operator
• To get a list of values that are equal to or lower than a value of your choice, use the is <= operator
• To get a list of values that are higher than a value of your choice, use the is > operator
• To get a list of values that are equal to or higher than a value of your choice, use the is >= operator

Both conditions can use the same operator or each can use a different operator. Create your combinations as you see fit to get the right list. Here is an example of a conjunction that the is > operator for one condition and the is < operator for another condition:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 500, 380, 664, 1285, 2548, 1000, 6320, 4 };

var number = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n is > 100 & n is < 2500
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

This would produce: In the same way, you can combine the Boolean operators in the conditions as you see fit. Here is an example that has a conjunction with one condition using the == operator while the other condition is using the the is < operator:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 25, 380, 6, 285, 2448, 32, 6320, 4 };

var number = from n
in numbers
where n % 5 == 0 & n is < 500
select n;

foreach (var member in number)
}
}
}```

This would produce: Once again, remember to include each condition in parentheses to make it easier to read. Therefore, the above code can be written as follows:

```private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 25, 380, 6, 285, 2448, 32, 6320, 4 };

var number = from n
in numbers
where (n % 5 == 0) & (n < 500)
select n;

foreach (var member in number)
}```

Or better yet:

```private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 25, 380, 6, 285, 2448, 32, 6320, 4 };

var number = from n
in numbers
where ((n % 5) == 0) & (n < 500)
select n;

foreach (var member in number)
}```

Remember that, to negate a conjunction or a condition, you can use the ! operator.

A Binary Conditional Conjunction

In the above sections, we used the binary logical conjunction represented by the & operator. The way that operator works is that it checks each condition of the conjunction. Normally, when the first condition produces a true result, you must check the second condition in case that first condition held or produced a false value. On the other hand, if the first condition is false, there is no reason to check the second condition because the wholse conjunction is going to be false regardless of the result of the second condition. To support this scenario, the C# language provides the double ampersand operator represented as &&. You can use this operator in place of the & exactly as we have done in previous sections.

AND for a Conjunction

We saw that you can use the is <, the is <=, the is >, and the is >= operators in the conditions of your conjunction. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 250, 380, 664, 1285, 2500, 15750, 6320, 4 };

var number     = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n is >= 250 & n is <= 2500
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

To make your code explicit, the C# language provides the and operator. It is used with the is operator. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 250, 380, 664, 1285, 2500, 15750, 6320, 4 };

var number     = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n is >= 250 and <= 2500
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

This would produce: To further make your code easier to read, put each combination of a logical operator and its accompanying value in parentheses. Here are examples:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 250, 380, 664, 1285, 2500, 15750, 6320, 4 };

var number     = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n is (>= 250) and (<= 2500)
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

Logical Disjunction

Introduction

A logical disjunction consists of finding a value that is excluded from two conditions. The C# language provides various operators to support this operation.

A Binary Logical Exclusion

A binary logical exclusion is a Boolean operation that considers two conditions so you can decide what to do if one condition is the opposite of the other. To support this operation, the C# language provides the ∧ operator. To apply the binary logical exclusion, create a where clause in your LINQ statement and separate two conoditions by this operator. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 250, 380, 664, 1285, 2500, 15750, 6320 };

var number     = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n <= 50 ^ n > 2000
select n;

foreach (var member in number)

foreach (var member in selections)
}
}
}```

This would produce: A Binary Logical Disjunction

A binary logical disjunction is a Boolean operation that considers two conditions to find out whether one of those conditions produces a false result. To perform this operation, use the | operator as done in C#. Here is an example:

```namespace Numerotation
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
var numbers = new double[] { 12, 445, 250, 380, 664, 1285, 2500, 15750, 6320 };

var number     = from n
in numbers
orderby n
select n;

var selections = from n
in numbers
where n <= 50 | n > 2000
select n;

foreach (var member in number)

foreach (var member in selections) 