WEBINAR:
On-Demand
Building the Right Environment to Support AI, Machine Learning and Deep Learning
Using the "where" Operator
Beyond the basic query operators you just saw, XLinq has operators such as the important
where operator that lets you filter query results based on supplied criteria. To demonstrate this, add a text box named
txtValue and a button named
btnGetProductNameForID to the page. As the name of the button suggests, clicking this button retrieves the product name for a specific product ID. The
Click event code for the new button looks like this:
 | |
Figure 2. Displaying a Product Name for a Specific Product ID: In this example, when a users enters a product ID and clicks the button, the application returns the corresponding product name. |
void btnGetProductNameForID_Click
(object sender, EventArgs e)
{
XElement products = XElement.Load
(Server.MapPath
("~/App_Data/Products.xml"));
var productNames = from prod in
products.Elements("Product")
where (string)prod.Attribute("ID")
== txtValue.Text
select (string) prod.Element("Name");
gridResult.DataSource = productNames;
gridResult.DataBind();
}
In this example, when a user enters a product ID into the txtValue text field, and clicks button, the "where" operator selects the product name that matches the supplied product ID.
Figure 2 shows the example in action.
Using the "Take" Operator
Similar to the
where operator, the
Take operator provides you with a way to select all the elements before some supplied position. For example, the following code snippet shows how to select the first five product names from the Products XML file.
Author's Note: Although the syntax for the "where" operator is different from the "Take" operatorwhich is obviously a methodthe documentation refers to both as "operators," so I have followed that lead in the terminology used for this article. |
void btnGetTop5ProductNames_Click
(object sender, EventArgs e)
{
 | |
Figure 3. Filtering the First Five Product Names: Here's the page in a browser, displaying the first five product names from the products list. |
XElement products = XElement.Load
(Server.MapPath
("~/App_Data/Products.xml"));
var productNames = (from prod in
products.Elements("Product")
orderby (string) prod.Element("Name")
select (string)
prod.Element("Name")).Take(5);
gridResult.DataSource = productNames;
gridResult.DataBind();
}
I won't go through the full description of how to set up an example page, but you can easily see what's involved by looking at
Figure 3.
Note how adding the
Take operator at the end of the query simply changes the output returned by the query. This just shows the power of LINQ as a query language.
Similarly, you can also get the unique list of product names using the
Distinct operator.
var productNames =
(from prod in
products.Elements("Product")
orderby (string) prod.Element("Name")
select (string)prod.Element
("Name")).Distinct();
The Distinct operator eliminates duplicate product names from the resultant output. In addition to the
Where,
Take, and
Distinct operators shown so far, LINQ exposes a number of other operators, the most important of which are shown in Table 1.
Table 1. Standard Query Operators: The table shows common query operators used for querying objects that are based on IEnumerable.
Operator |
Description |
select |
Specifies the return value for the query. |
where |
Allows you to filter the output. |
First |
Retrieves the first member from the resultant list. |
ElementAt |
Allows you to access the specific member at specified position. |
Take |
Accesses the members before the specified position. |
Skip |
Accesses the members after the specified position. |
ToDictionary |
Allows you to return the results in a key pair value list. |
orderby |
Allows sorting of the resultset in ascending order. |
orderbyDescending |
Allows sorting of the resultset in descending order. |
Reverse |
Allows you to reverse the order of sequence. |
Distinct |
Allows you to filter duplicate members. |
ToList |
Allows you to return the results as a list represented by List<T>. |
Except |
Allows you to filter elements that are members of a specific set. |