Sunday 12 February 2012

Applying Criteria to a LINQ Statement



Where is the Condition?



Simply using a select statement produces the same list of values as the original. A criterion is a condition applied to a set of values to find out which one(s) respond(s) to the condition or validate the condition. When applied to a list, a criterion examines each member, finds out what member responds to it 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 the Where operator. The formula to use is:

var SubListName = from ValueHolder in List where Condition select ValueHolder;

In the new section, the where keyword is required. The Condition is formulated using one of the logical operators you are already familiar with from the C# language. It can be in the form of:
  • ValueHolder == SomeValue: If ValueHolder is the same as SomeValue, the list member that is being examined is valid
  • ValueHolder < SomeValue: If ValueHolder is less than SomeValue, the list member that is being examined is valid
  • ValueHolder <= SomeValue: If ValueHolder is the same as, or lower than, SomeValue, the list member that is being examined is valid
  • ValueHolder > SomeValue: If ValueHolder is greater than SomeValue, the list member that is being examined is valid
  • ValueHolder >= SomeValue: If ValueHolder is the same as, or greater than, SomeValue, the list member that is being examined is valid
  • ValueHolder != SomeValue: If ValueHolder is different from SomeValue, the list member that is being examined is valid
  • !(ValueHolder == SomeValue): If ValueHolder is different from SomeValue, the list member that is being examined is valid
Remember that you must create the criterion as a Boolean operation. After applying the select statement, you can then use it as you see fit. 

Here is an example:
using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

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

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

This would produce:
LINQ Where

Of course, the purpose of querying a list is to isolate one or more values. As such, you can create an expression that checks a value and applies some condition to it. For example, in this list of numbers, you may want to find out whether it contains one or more numbers that are divisible by 5. This operation can be carried by the % operator as in "number % 5"; but number % 5 is pure algebra, not Boolean. Therefore, you must add a condition to make it a valid Boolean expression. For example, you can find out if the number % 5 operation is equal to 0.

 Here is an example:
using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

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

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

This would produce:
LINQ Where

To make the statement easier to read and less confusing, you should make it a habit to isolate the groups of statements in parentheses:
using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

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

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

After applying a where condition, you can sort the list using an orderby operator. For example, to get a list of odd numbers arranged in numerical order, you would write the statement as follows:
 
using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

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

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

This would produce:
LINQ Where

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, 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:
using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

        var number = from n
                     in numbers
                     where !((n % 5) == 0)
                     select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

This would produce:
LINQ Not

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

No comments :