This chapter demonstrates about the LINQ Queries. Which is a programming language construct and acts upon some Data Source and also return some value or Data. Following are the concepts covered in this chapter.
Obtaining Data Source
creating the Query
Exciting the Query
Obtaining Data Source
Description
In order to get the LINQ to the Data Source user required querabledatatype without any modifications. If Data Source is not present in a memory as a querable type then LINQ need s to represent the source Ex:LINQ loads XML Document into XElement type. The snippet code demonstrates the data source from an XML Document.
[csharp]
// Create a data source from an XML document.
// using System.Xml.Linq;
XElement contacts = XElement.Load(@"c:\myContactList.xml");
[/csharp]
In order to get the relation between the LINQ and SQL user need to create the object-relational mapping at the designing time and user can create those model by manually or by using the object relational Designer in Visual Studio. The User can write the at run time against the objects then LINQ to SQL handles the communication by using the database. The snippet code below demonstrates the Customers table.
[csharp]
/ Query for customers in London.
IQueryable<Customer> custQuery =
from cust in db.Customers
where cust.City == "London"
select cust;
[/csharp]
creating the Query
Description
Query contains the information about how to retrieve the information from the sources and also contains how the data should be grouped, stored, shaped. Which stores the data in query variable format and initialised with a query expression.
In LINQ , a query always will work on some data source i.e inherited from "IEnumerable interface". A LINQ Query can contain 3 parts is as listed below.
From Clause
Is used specify the Data Source.
Where Clause
Is used to apply the filter.
Select Clause
Is used to specify the returned element type.
Exciting the Query
Description
To execute the query there are two methods available in LINQ those are useful to get the data from the different sources by using queries these 2 methods are listed below.
Deferred Execution
Forced immediate execution
Deferred Execution
Description
In Deferred execution, query will be executed when a foreach loop is executed. As long as loop is being iterated when query will be running which is referred as deferred execution. the snippet code below demonstrates the Deferred Execution.
[csharp]
// Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
[csharp]
Query will not holds the result data user need to execeute it. For example there is a 2 databases in one database the values are updating continuously by a seperate application then user need to create the query which retreive the latest updated data repeatedly.
Forced immediate execution
"Description"
Queries which perform the aggregation function on a source elements. For example some queries are Count, Average, Max, and First which are execute externally foreach statement is used to return a result and is returns only single value not an IEnumerable collection. The code below demonstrates the Forcing immediate execution.
[csharp]
var evenNumQuery =
from num in numbers
where (num % 2) == 0
select num;
int evenNumCount = evenNumQuery.Count();
[/csharp]
[csharp]
List<int> numQuery2 =
(from num in numbers
where (num % 2) == 0
select num).ToList();
// or like this:
// numQuery3 is still an int[]
var numQuery3 =
(from num in numbers
where (num % 2) == 0
select num).ToArray();
[/csharp]
Summary
Points
Data Source Implicitly supports generic IEnumerable interface.
Force execution executed by putting the for each loop.