Book Image

LINQ Quickly

By : N Satheesh Kumar
Book Image

LINQ Quickly

By: N Satheesh Kumar

Overview of this book

<p>This book gets you started with LINQ and shows how it will make your programming life easier by making use of new features from the .NET Framework 3.0. This book is split into seven chapters, each of which is dedicated to presenting a feature of LINQ and its usage in real-life scenarios. <br /><br />Language Integrated Query (LINQ) is a new feature in Visual Studio 2008 that extends its query capabilities, using C# and Visual Basic. Visual Studio 2008 comes with<br />LINQ provider assemblies that enable the use of LINQ with data sources such as in-memory collections, SQL relational databases, ADO.NET Datasets, XML documents, etc.<br />In Visual Studio 2008, Visual C# and Visual Basic are the languages that implement the LINQ language extensions. LINQ language extensions use the new standard query operators API, which is the query language for any collection that implements IEnumerable&lt;T&gt;.</p>
Table of Contents (14 chapters)
LINQ Quickly
Credits
About the Author
About the Reviewer
Preface
Building an ASP.NET Application
LINQ with Outlook

Querying Datasets


LINQ provides many query operators and custom operators with which we can query DataSets. When we say querying DataSets, we actually mean querying DataTables inside DataSets. We cannot directly query DataTables, as it returns DataRow objects. To be a part of LINQ queries, DataTables should be enumerable and the source for the LINQ query should be IEnumerable<T>. Querying can be done on enumeration of DataRow objects so that we will have all DataColumns available for the query expressions.

var categories = dataSetDeserts.Tables[0].AsEnumerable();
var items = dataSetDeserts.Tables[1].AsEnumerable();
var rowCategories = from p in categories
where p.Field<int>("CategoryID") == 1
select p;
foreach (var cat in rowCategories)
{
Console.WriteLine(cat[0] + " " + cat[1] + " " + cat[2]);
}

In the above example, dataSetDeserts has two tables which have details of different categories and items for each category. To query these details, we need to get the enumeration of...