Using a Criterion to Select a List

Where is the Condition

When you create a simple select statement in LINQ, the compiler produces the same list of values in the array. A criterion is a condition applied to a set of values to find out which one(s) respond(s) to one or more conditions. When applied to a list, a criterion examines each member, finds out what member responds to the condition and, if so, adds that member to the from list.

To apply a criterion, you create a Boolean operation between the in statement and the select statement. This criterion is actually formulated using an operator named where. The formula to use is:

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

Of course, to make your code easy to read, you can distribute the statement to different lines as follows:

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

In the new section, the where keyword is required. The condition is formulated using a logical operator.

A Comparison for Equality

To compare each of the values of a list to a certain value to find out which one(s) is(are) the same as a value of your choice, you can use the == operator. The formula to follow is:

value-holder == some-value

If value-holder is the same as some-value, the list member that is being examined is valid. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n == 445
                         select n;

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

This would produce:

Numbers

You should avoid using the == operator on decimal values because it is difficult and can be unpredictable when comparing two decimal numbers (such as 12.55 and 12.5503 or 12.55 and 12.5582) for equality.

Finding Values Not Equal

Instead of finding values that are equal to a constant you have, you may be insterested in values that are different from that one. To assist you with this operation, the C# language and LINQ support the != operator that can be used in a formula as follows:

value-holder != some-value

If the value-holder is different from some-value, the compared item of the list is selected for the result. The != operator can be applied to all types of numbers. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n != 445
                         select n;

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

This would produce:

Numbers

Sorting a List

Remember that, after getting a LINQ list, you can arrange the resulting list in an order of your choice. To do this, add an orderby clause after the where condition. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n != 445
                         orderby n
                         select n;

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

This would produce:

Numbers

Comparisons on Decimal Numbers

A Comparison for a Lower Value

Imagine you have a list of values and you want to find out which ones are lower than a value of your choice, as seen in C#, you can use the is < operator. The formula to follow is:

value-holder [is] < some-value

If the value-holder is lower than some-value, the list member that is being examined is valid. This operator can be applied to numbers of all kinds. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n is < 445
                         select n;

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

This would produce:

Numbers

The is keyword is used to make your code easy to read and understand, but it is optional. This means that you can omit the is keyword and your code would work just fine. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n < 445
                         orderby n
                         select n;

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

This would produce:

Numbers

A Comparison for a Lower or Equal Value

If you have a list of values and you want to find out which ones are lower than or equal to a value of your choice, you can compare the values using the is <= operator. The formula to follow is:

value-holder [is] <= some-value

If the value-holder is the same as, or lower than, some-value, the list member that is being examined is valid. This operator can be applied to integers. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n is <= 445
                         select n;

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

This would produce:

Numbers

Although you can, you should avoid using the is <= operator on decimal numbers for the same reason we gave for ==. The is keyword is optional.

A Comparison for a Greater Value

If you want to get a list of values that are greater than a certain value you have, you can use the is > operator. The formula to use it is:

value-holder [is] > some-value

If the value-holder is greater than some-value, the list member is selected. This operator can be applied to numbers of all kinds. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n is > 285
                         select n;

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

This would produce:

Numbers

A Comparison for a Greater or Equal Value

Sometimes, you want to compare a constant value to values of a list to find out which one(s) of those values is (are) equal or higher than your value. To perform this operation, you use the is >= operator as follows:

value-holder [is] >= some-value

If the value-holder is the same as, or greater than, some-value, the compared item of the list is selected. This operator can be applied to integers. 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, 6, 285, 2448, 32, 6320, 4 };

            var number = from n
                         in numbers
                         where n is >= 285
                         select n;

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

This would produce:

Numbers

Finding the Remainders from a List

If you have a number, you may want to find out whether it is divisible by another number of your choice. This operation is referred to as finding the remainder of a number. The operation is performed with the % operator. You can also apply the operation to a series of numbers. To perform this operation in LINQ, you must add a condition to make it a valid Boolean expression. Here is an example:

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
                 select n;

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

This would produce:

Numbers

To make the statement easier to read and less confusing, you should isolate the groups of statements in parentheses:

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
                 select n;

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

Negating a Condition

Introduction

You can create a criterion that works perfectly but rather want its opposite. To get it, you can negate the expression. To do this, you use the ! operator of the C# language. To make the expression easy to read, you should include it in parentheses. That is, put the ! operator in the beginning of the logical expression followed by parentheses that include the actual logical expression. Here is an example:

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)
                 select n;

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

This would produce:

Numbers

In the same way, you can negate a where condition that involves any appropriate value.

Remember that, after applying a where condition, you can sort the list using an orderby operator. Here is an example that gets a list of odd numbers arranged in numerical order, you would write the statement as follows:

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

    var number = from n
                 in numbers
                 where n % 2 != 0
                 orderby n ascending
                 select n;

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

This would produce:

Numbers

Where a Condition IS NOT Valid

We have already seen that, when you are creating a criterion that compares values for lower or higher values, you can combine the is keyword and the Boolean operator. To let you negate such a condition, the C# language provides a keyword named not. You write it between the is keyword and the 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, 25, 380, 616, 285, 2448, 32, 6320, 4 };

            var original = from n
                           in numbers
                           where n is <= 285
                           select n;

            foreach (var member in original)
                lbxConditions.Items.Add(member.ToString());

            var negation = from n
                           in numbers
                           where n is not <= 285
                           select n;

            foreach (var member in negation)
                lbxNegations.Items.Add(member.ToString());
        }
    }
}

This would produce:

Numbers

Nesting a Conditional Query

Consider a query as follows:

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

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

            var original = from nbr
                           in numbers
                           select nbr;

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

            var conditioned = from n
                              in numbers
                              where n is not <= 285
                              select n;

            foreach (var member in conditioned)
                lbxSelections.Items.Add(member.ToString());
        }
    }
}

This would produce:

Numbers

Remember that a query can contain a condition as in the above example. In your query, in place of the list, you can create a local query. The condition would then apply to the nested query. Here is an example:

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

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

            var original = from nbr
                           in numbers
                           select nbr;

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

            var conditioned = from val
                              in from nbr
                                 in numbers
                                 select nbr
                              where val is not <= 285
                              select val;

            foreach (var member in conditioned)
                lbxSelections.Items.Add(member.ToString());
        }
    }
}

Previous Copyright © 2014-2023, FunctionX Friday 04 November 2022 Next