Simply using a select statement as done above 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 a condition to validate one or some values. 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 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:
Remember that you must create the criterion as a Boolean operation. After applying the select statement, you can use it as you see fit. Here is an example: 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 == 445
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:
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: 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
select n;
foreach (var member in number)
lbxNumbers.Items.Add(member.ToString());
}
This would produce:
To make the statement easier to read and less confusing, you should isolate the groups of statements in parentheses: 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
select n;
foreach (var member in number)
lbxNumbers.Items.Add(member.ToString());
}
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 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)
select n;
foreach (var member in number)
lbxNumbers.Items.Add(member.ToString());
}
This would produce:
In the same way, you can negate a where condition that involves any appropriate value. |
|
|||||||
|