Home

Logical Conjunction and Disjunction in LINQ

     

Conjunction

You can use a logical conjunction to combine two Boolean expressions. To do this, in the where statement, instead of one, create and many conditions as you want. Between each combination of two conditions, include the conjunction operator. As you may know already, a conjunction is C# is done using the && operator. The operator can be implemented in LINQ as follows:

using System;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

public class Exercise : Form
{
    private ListBox lbxNumbers;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        lbxNumbers = new ListBox();
        lbxNumbers.Location = new System.Drawing.Point(12, 12);
        lbxNumbers.Width = 100;

        Text = "Numbers";
        MinimizeBox = false;
        MaximizeBox = false;
        Controls.Add(this.lbxNumbers);
        Load += new EventHandler(ExerciseLoad);
        Size = new System.Drawing.Size(130, 145);
        StartPosition = FormStartPosition.CenterScreen;
    }

    private void ExerciseLoad(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)
            lbxNumbers.Items.Add(member.ToString());
    }

    [STAThread]
    public static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Numbers

Notice how difficult this statement is confusing to read. An alternative is to include each Boolean statement in parentheses to make it easier to read. This can be done as follows:

private void ExerciseLoad(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)
        lbxNumbers.Items.Add(member.ToString());
}

Or better yet:

private void ExerciseLoad(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)
        lbxNumbers.Items.Add(member.ToString());
}

To negate a conjunction, precede it with a ! operator. Remember that a conjunction is made of two parts. Therefore, if you want to negate only the first part, precede it with !. If you want to negate the whole conjunction, put the conjunction in parentheses but precede it with ! (outside the parentheses).

 
 
 

Disjunction

A logical disjunction consists of finding a value that is excluded from two conditions. In C# and LINQ, this is done using the disjunction operator, which is ||. To use this operator, create the conditions and separate them with the operator. Here is an example:

private void ExerciseLoad(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 <= 50) || (n > 2000)
                 select n;

    foreach (var member in number)
        lbxNumbers.Items.Add(member.ToString());
}

This would produce:

Numbers

 
   
 

Previous Copyright © 2014-2016, FunctionX Next