four strings into the tasks queue:
tasks.Enqueue('Do homework');
tasks.Enqueue('Phone rings');
tasks.Enqueue('Get changed');
tasks.Enqueue('Go for movies');
To retrieve the elements from a queue, you can use either the Peek()
method or the Dequeued
method. Peek()
returns the object at the beginning of the queue without removing it. Dequeue()
removes and returns the object at the beginning of the queue:
Console.WriteLine(tasks.Peek()); //---Do homework---
Console.WriteLine(tasks.Dequeue());-- //---Do homework---
Console.WriteLine(tasks.Dequeue());-- //---Phone rings---
Console.WriteLine(tasks.Dequeue());-- //---Get changed---
Console.WriteLine(tasks.Dequeue());-- //---Go for movies---
If a queue is empty and you try to call the Dequeue()
method, an InvalidOperationException
error occurs, so it is useful to check the size of the queue using the Count
property before you perform a dequeue operation:
if (tasks.Count < 0)
Console.WriteLine(tasks.Dequeue());
else
Console.WriteLine('Tasks is empty');
To extract all the objects within a Queue
object without removing the elements, use the foreach statement, like this:
foreach (string t in tasks) Console.WriteLine(t);
Here's what prints out:
Do homework
Phone rings
Get changed
Go for movies
Summary
This chapter explained how to manipulate data using arrays. In addition, it explored the System.Collections
namespace, which contains the various interfaces that define basic collection functions. It also contains several useful data structures, such as a dictionary, stacks, and queues, that greatly simplify managing data in your application.
Chapter 14
Language Integrated Query (LINQ)
One of the most exciting new features in the .NET Framework v3.5 is the Language Integrated Query (LINQ). LINQ introduces to developers a standard and consistent language for querying and updating data, which include objects (such as arrays and collections), databases, XML documents, ADO.NET DataSets, and so forth.
Today, most developers need to know a myriad of technologies to successfully manipulate data. For example, if you are dealing with databases, you have to understand Structured Query Language (SQL). If you are dealing with XML documents, you must understand technologies such as XPath, XQuery, and XSLT. And if you are working with ADO.NET DataSets, then you need to know the various classes and properties in ADO.NET that you can use.
A better approach would be to have a unified view of the data, regardless of its form and structure. That is the motivation behind the design of LINQ. This chapter provides the basics of LINQ and shows how you can use LINQ to access objects, DataSets, and XML documents, as well as SQL databases.
LINQ Architecture
Figure 14-1 shows the architecture of LINQ. The bottom layer contains the various data sources with which your applications could be working. On top of the data sources are the LINQ- enabled data sources: LINQ to Objects, LINQ to DataSet, LINQ to SQL, LINQ to Entities, and LINQ to XML. LINQ-enabled data sources are also known as LINQ providers; they translate queries expressed in Visual Basic or C# into the native language of the data source. To access all these data sources through LINQ, developers use either C# or Visual Basic and write LINQ queries.

Figure 14-1
LINQ to Entities is beyond the scope of this book. It was slated to be released later in 2008 and is not part of Visual Studio 2008.
So how does your application view the LINQ-enabled data sources?
□ In LINQ to Objects, the source data is made visible as an IEnumerable<T>
or IQueryable<T>
collection.
□ In LINQ to XML, the source data is made visible as an IEnumerable<XElement>
.
□ In LINQ to DataSet, the source data is made visible as an IEnumerable<DataRow>
.
□ In LINQ to SQL, the source data is made visible as an IEnumerable
or IQueryable
of whatever custom objects you have defined to represent the data in the SQL table.
LINQ to Objects
Let's start with LINQ to Objects. It enables you to use LINQ to directly query any IEnumerable<T>
or IQueryable<X>
collections (such as string []
, int[]
, and List<T>
) directly without needing to use an immediate LINQ provider or API such as the LINQ to SQL or LINQ to XML.
Say that you have a collection of data stored in an array, and you want to be able to retrieve a subset of the data quickly. In the old way of doing things, you write a loop and iteratively retrieve all the data that matches your criteria. That's time-consuming because you have to write all the logic to perform the comparison and so on.