31
Visual Studio 2008 Community Training By Mohamed Saleh [email protected] www.jordev.net www.geeksconnected.com/mohamed

Module 3: Introduction to LINQ (Material)

Embed Size (px)

DESCRIPTION

Module 3: Introduction to LINQ (Material)Jordan .NET User Group (Jordev)Community MaterialMohamed Saleh

Citation preview

Page 1: Module 3: Introduction to LINQ (Material)

Visual Studio 2008 Community Training

By

Mohamed Saleh

[email protected]

www.jordev.net

www.geeksconnected.com/mohamed

Page 2: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

2

MODULE 3: INTRODUCTION TO LINQ

Table of Contents

Official Community Material License 3

Module Overview 4

Introducing LINQ Project 5

LINQ Assemblies 6

LINQ Query Expressions 7

Lab 1: Writing Query Expressions 9

Standard Query Operators 12

Lab 2: Writing Queries Using Extension Methods 13

Conversion Operators 16

Lab 3: Using Conversion Operators 17

Partitioning Operators 21

Lab 4: Using Partitioning Operators 22

LINQ Bridge 26

Lab 5: Using Linq for .NET Framework 2.0 27

Summary 30

References 31

Page 3: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

3

Official Community Material License

While every precaution has been taken in the preparation of this document, Jordev assumes no responsibility for

errors, omissions, or for damages resulting from the use of the information herein.

This disclaimer statement, presented on this page, is an integral part of this document. Should this page of the

document, in its electronic or printed formats, be lost, deleted, destroyed or otherwise discarded, this disclaimer

statement applies despite the consequences.

© 2008 Jordev.NET. All rights reserved.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Page 4: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

4

Module Overview

This module introduces you to the new generation of the programmatic data access in

the .NET Framework 3.5. LINQ Project is a new movement in the data access

programming world. It helps the developers to deal with different data shapes into the

programming language directly without the need to learn new API(s) for each data

shape and different type of information.

This module describes the new extensions that makes the LINQ happened, the query

syntax, the standard query operators, and how to use LINQ with the in-memory

collections.

Objectives:

After completing this module, you will be able to:

Understand what Linq is.

Understand the Query Expressions and Query Operators.

Writing queries using extension methods.

Using the Partitioning and Conversion operators.

Explain the usage of the new Linq Assemblies.

Enabling Linq to Objects for .Net Framework 2.0.

Page 5: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

5

Introducing LINQ Project

Overview:

In the FoxPro/dBase days the data manipulation operations and queries was integrated with

the programming language itself, and it was supported natively in the language, then the

Client/Server computing separates the data and the programming language, and the object

oriented technology become very mature and richer than before and empowered very much

from the programming language itself.

The complexity of programming happens when dealing with data, and especially these days

we have different kind of data such as RDBMS, XML, file system, etc…

LINQ adds new concepts to the C# and VB Language which allows the programmers to write

SQL-Like query expressions that can execute many operations over much kind of data while

using the object oriented programming style.

LINQ Building Blocks:

The LINQ expressions are enabled because of the marriage between different new features in

the C# 3.0 Language which includes the following:

1. Object Initializers

2. Lambda Expressions

3. Extension Methods

4. Anonymous types

5. Implicit Typing Variables

As we mentioned in the C# 3.0 module, the C# 3.0 features are only related to the compiler

and it will not reflect any new IL instructions.

All of these new features considered as the building blocks of the LINQ, which combines

them together and enable the LINQ.

Page 6: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

6

LINQ Assemblies

The following table contains the LINQ assemblies with the main namespaces:

No. Assembly Name Namespaces

1. System.Core.dll System

System.Linq

System.Linq.Expressions

2. System.Data.DataSetExtensions.dll System.Data

3. System.Data.Linq.dll System.Data.Linq

System.Data.Linq.Mapping

System.Data.Linq.SqlClient

4. System.Xml.Linq.dll System.Xml.Linq

System.Xml.Schema

System.Xml.XPath

The first assembly System.Core provides extensions methods for IEnumerable<T> and

some classes for query operators in addition to other classes that enable the query expressions.

The second assembly System.Data.DataSetExtensions allows the using of LINQ for

DataSet.

The System.Data.Linq assembly is the implementation of LINQ for SQL Server databases

which contains many classes that represent the tables as entities and provides some attributes

for the tables and columns, in addition to some helpers and methods for the SQL statements.

The System.Xml.Linq is the implementation of LINQ for XML which allows the

programmer to construct Xml documents using LINQ expressions and query it such as any

other data.

Page 7: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

7

LINQ Query Expressions

Overview:

Query expressions can be written using declarative query syntax enabled by the C# 3.0

language enhancements. Query expressions can be used to transform data from any Linq-

enabled data sources into different Linq-Enabled data source.

Query:

A query is a set of instructions that describes what data to retrieve from a given data source

(or sources) and what shape and organization the returned data should have. (Ref 2)

Query Expression:

A query expression is a query expressed in query syntax. A query expression is a first-class

language construct. A query expression consists of a set of clauses written in a declarative

syntax similar to SQL or XQuery. Each clause in turn contains one or more C# expressions,

and these expressions may themselves be either a query expression or contain a query

expression. (Ref 2)

Query Expression Syntax:

The following sample shows a simple query expression:

string[] members = {"Samer", "Bill", "Scott", "Bilal"};

var names = from n in members

where n.StartsWith("B")

select n;

Page 8: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

8

The names represents the query variable that store the query, the query expression must begin

with from clause and must end with select/group clause. Between the from and select clause

it can contains optional clauses such as where, join, or orderby.

Query Operators:

Query operators are static extension methods that construct the query expressions, the

following statements shows how to write query expressions using the extension methods

directly:

string[] members = { "Samer", "Bill", "Scott", "Bilal" };

var names = members.Where(m => m.StartsWith("B"));

The two samples are identical but the first sample gives more query-style syntax. there are

many different query operators shipped with the Linq, in this module you will do samples

over many basic query operators.

IEnumerable<T> is must:

The var define implicit typing query result, but all the results must be returned in a type that

implements an IEnumerable<T> type.

The following sample reduces the same results as the first sample:

string[] members = {"Samer", "Bill", "Scott", "Bilal"};

IEnumerable<string> names = from n in members

where n.StartsWith("B")

select n;

Query Execution:

The query can be executed immediately when using the ToArray or ToList query operators

and can be done lately - which is the default case - which called deferred query execution or

the lazy execution. The deferred query execution enables the reusability of query even if the

data changed.

Page 9: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

9

Lab 1: Writing Query Expressions

After completing this lab, you will be able to:

Writing Linq Query Expression.

Using Query Operators.

Using var and IEnumerable<T> query varaiables.

Querying In-Memory Collections.

Writing Query Expressions

1. On the File menu in Visual Studio 2008, point to New and then click Project.

2. In the New Project dialog box, select a language, and then select Windows in the

Project Types box.

3. In the Templates box, select Console Application.

4. In the Location box, type the path to where to create the application, and then click

OK.

5. In the Visual Studio code editor as the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace GeeksConnected.Module03Lab01

{

class Customer

{

Page 10: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

10

public long ID { get; set; }

public string Name { get; set; }

public string Company { get; set; }

public Phone phone { get; set; }

public Customer()

{

}

}

class Phone

{

public int CountryCode;

public int AreaCode;

public int phone;

public Phone()

{

}

}

class Program

{

static void Main(string[] args)

{

List<Customer> custList = new List<Customer>

{

new Customer

{

Name = "Samer", ID = 1, Company = "Microsoft",

phone = new Phone { CountryCode = 001, AreaCode = 6,

phone = 111111 }

},

new Customer

{

Name = "Muhanad", ID = 2, Company = "GeeksConnected",

phone = new Phone { CountryCode = 001, AreaCode = 6,

phone = 111111 }

},

new Customer

{

Name = "Mohamed", ID = 2, Company = "GeeksConnected",

phone = new Phone { CountryCode = 962, AreaCode = 9,

phone = 2324343 }

},

new Customer

{

Name = "Amjad", ID = 2, Company = "GeeksConnected",

phone = new Phone { CountryCode = 962, AreaCode = 43,

phone = 3264843 }

},

new Customer

{

Name = "Bill", ID = 1, Company = "Microsoft",

phone = new Phone { CountryCode = 001, AreaCode = 6,

phone = 875654 }

}

};

//Microsoft Customers List

Page 11: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

11

IEnumerable<Customer> MicrosoftCustomers =

from c in custList

where (c.Company == "Microsoft")

select c;

Console.WriteLine("Microsoft Customers");

Console.WriteLine("===================");

foreach (Customer cus in MicrosoftCustomers)

{

Console.WriteLine("ID : {0}",cus.ID);

Console.WriteLine("Name: {0}", cus.Name);

Console.WriteLine("====================\n");

}

Console.ReadLine();

//GeeksConnected Customers List

var GeeksConnectedCustomers =

from c in custList

where c.Company.StartsWith("Geeks")

select c;

Console.WriteLine("GeeksConnected Customers");

Console.WriteLine("===================");

foreach (Customer cus in GeeksConnectedCustomers)

{

Console.WriteLine("ID : {0}", cus.ID);

Console.WriteLine("Name: {0}", cus.Name);

Console.WriteLine("====================\n");

}

Console.ReadLine();

//Searching by Country Code

IEnumerable<Customer> CustomersByCountryCode =

from c in custList

where (c.phone.CountryCode == 001)

select c;

Console.WriteLine("001 Country Code Customers");

Console.WriteLine("===================");

foreach (Customer cus in CustomersByCountryCode)

{

Console.WriteLine("ID : {0}", cus.ID);

Console.WriteLine("Name: {0}", cus.Name);

Console.WriteLine("====================\n");

}

Console.ReadLine();

}

}

}

6. Click Start on the Debug menu or press F5 to run the code.

Page 12: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

12

Standard Query Operators

Enumerable Extension Methods:

Linq shipped with a lot of extension methods which act as the query operators when writing

query expressions.

The System.Linq namespace contains different Linq Extensions under the

System.Linq.Enumerable class, the extension methods can be consumed through the using

directive, and by default the projects in .NET Framework 3.5 are using the System.Linq

namespace.

The following table contains the some commonly used operators:

No. Extension Method Description

1. OrderByDescending Sorting the elements in a descending order.

2. OrderBy Sorting the elements in a Ascending order.

3. Take Returns a specific number of continuous elements from the start of a

sequence of values.

4. Sum Calculate the sum of a group of numeric values.

5. Distinct Returns distinct element from a sequence of values.

6. Count Return the total number of elements from a sequence of elements.

7. First Returns the first element of a sequence of values.

Page 13: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

13

Lab 2: Writing Queries Using Extension Methods

After completing this lab, you will be able to:

Writing Queries using Linq Extension Methods.

Sorting the elements using the OrderByDescending extension method.

Returning specific elements using Take and First methods.

Calculating the total number of elements using the Count method.

Returning Distinct values from a sequence of values.

Writing Queries using extension methods

1. On the File menu in Visual Studio 2008, point to New and then click Project.

2. In the New Project dialog box, select a language, and then select Windows in the

Project Types box.

3. In the Templates box, select Console Application.

4. In the Location box, type the path to where to create the application, and then click OK.

5. In the Visual Studio code editor as the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace GeeksConnected.Module03Lab02

{

class Book

{

public long ID { get; set; }

Page 14: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

14

public string Name { get; set; }

public string Author { get; set; }

public string Publisher { get; set; }

public int Year { get; set; }

public string Category { get; set; }

public Book()

{

}

}

class Program

{

public static List<Book> GetBooksList()

{

List<Book> BooksList = new List<Book>

{

new Book {ID = 1, Author = "Muhanad", Category = "SharePoint",

Name = "Inside MOSS Customization", Publisher = "GC",

Year = 2008},

new Book {ID = 2, Author = "Mohamed", Category = ".NET",

Name = "Advanced .NET", Publisher = "GC",

Year = 2008},

new Book {ID = 3, Author = "Amjad", Category = "Integration",

Name = "Advanced BizTalk", Publisher = "GC",

Year = 2008},

new Book {ID = 4, Author = "Hikmet", Category = "Windows",

Name = "Windows 2008 Server", Publisher = "GC",

Year = 2008},

new Book {ID = 5, Author = "Ayman", Category = "Inegration",

Name = "BIZTalk Administration", Publisher = "DN",

Year = 2006},

new Book {ID = 6, Author = "Ayman", Category = "SharePoint",

Name = "Programming CAML", Publisher = "DN",

Year = 2005},

new Book {ID = 7, Author = "Bob", Category = ".NET",

Name = "CLR Inside Outside", Publisher = "ORA",

Year = 2005},

new Book {ID = 8, Author = "Ibrahim", Category = "Inegration",

Name = "BIZTalk PipeLines", Publisher = "DHC",

Year = 2005},

new Book {ID = 9, Author = "Adam", Category = ".NET",

Name = "MSIL For Dummies", Publisher = "DHC",

Year = 2006},

new Book {ID = 10, Author = "Salim", Category = ".NET",

Name = "CLR Deep Dive", Publisher = "DN",

Year = 2006},

new Book {ID = 11, Author = "Hikmet", Category = "Windows",

Name = "Exchange Migration", Publisher = "MS",

Year = 2007},

new Book {ID = 12, Author = "Muhanad", Category = "SharePoint",

Name = "WSS Solutions", Publisher = "MS",

Year = 2007},

};

return BooksList;

}

static void Main(string[] args)

{

List<Book> books = GetBooksList();

//Getting Books Count...

int booksCount = books.Count();

Console.WriteLine("Books Count: {0}", booksCount);

Page 15: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

15

Console.ReadKey();

//Order Books Years By Descending...

var BooksList = books.OrderByDescending(b => b.Year);

//Printing the Books By Year

Console.WriteLine("Books By Year");

foreach (Book bk in BooksList)

{

Console.WriteLine("Book Name:{0}", bk.Name);

Console.WriteLine("Book Year:{0}", bk.Year);

Console.WriteLine("=======================\n");

}

Console.ReadKey();

//Returnning the first three books

var Bookslst = books

.OrderBy(b => b.Year)

.Take(3);

Console.WriteLine("\n\nFirst Three Books");

Console.WriteLine("=================\n");

foreach (Book bk in Bookslst)

{

Console.WriteLine("Book Name :{0}", bk.Name);

Console.WriteLine("Book Year :{0}", bk.Year);

Console.WriteLine("Book Author:{0}", bk.Author);

Console.WriteLine("=======================\n");

}

Console.ReadKey();

//Returnning the Distinct Values of Author

var bks = books

.OrderBy(b => b.Author)

.Select(b => b.Author)

.Distinct();

Console.WriteLine("\n\nDistinct Authors");

Console.WriteLine("=================\n");

foreach (var bk in bks)

{

Console.WriteLine("Book Author:{0}", bk);

Console.WriteLine("=======================\n");

}

Console.ReadKey();

}

}

}

6. Click Start on the Debug menu or press F5 to run the code.

Page 16: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

16

Conversion Operators

Linq can convert a query results to different data collections types, as we mentioned before

the default execution behavior of the query is the deferred execution, which execute the query

and retrieve the results while iterating through the sequence of values results.

The following operators aim to convert the results to different collection types, and cause an

immediate execution to the query.

No. Query Operator Description

1. ToArray Returns an array of elements from a sequence of values.

2. ToList Returns a List<T> of elements from a sequence of values.

3. ToDictionary Returns a Dictinory(TKey, TValue) collection type from a sequence

of values.

4. OfType Returns a filtered sequence of elements based on a specific type.

Page 17: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

17

Lab 3: Using Conversion Operators

Objectives:

This lab introduces you the query conversion operators which is a set of extension methods on

the Enumerable Class, in addition to enforce immediate execution behavior of the queries.

After completing this lab, you will be able to:

Enforce the immediate execution of the queries.

Converting the query results to Different Collection Types.

Filtering the query results based on a specific data type.

Using conversion Operators

1. On the File menu in Visual Studio 2008, point to New and then click Project.

2. In the New Project dialog box, select a language, and then select Windows in the

Project Types box.

3. In the Templates box, select Console Application.

4. In the Location box, type the path to where to create the application, and then click OK.

5. In the Visual Studio code editor as the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

Page 18: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

18

namespace GeeksConnected.Module03Lab02

{

class Book

{

public long ID { get; set; }

public string Name { get; set; }

public string Author { get; set; }

public string Publisher { get; set; }

public int Year { get; set; }

public string Category { get; set; }

public Book()

{

}

}

class Program

{

public static List<Book> GetBooksList()

{

List<Book> BooksList = new List<Book>

{

new Book {ID = 1, Author = "Muhanad", Category = "SharePoint",

Name = "Inside MOSS Customization", Publisher = "GC",

Year = 2008},

new Book {ID = 2, Author = "Mohamed", Category = ".NET",

Name = "Advanced .NET", Publisher = "GC",

Year = 2008},

new Book {ID = 3, Author = "Amjad", Category = "Integration",

Name = "Advanced BizTalk", Publisher = "GC",

Year = 2008},

new Book {ID = 4, Author = "Hikmet", Category = "Windows",

Name = "Windows 2008 Server", Publisher = "GC",

Year = 2008},

new Book {ID = 5, Author = "Ayman", Category = "Inegration",

Name = "BIZTalk Administration", Publisher = "DN",

Year = 2006},

new Book {ID = 6, Author = "Ayman", Category = "SharePoint",

Name = "Programming CAML", Publisher = "DN",

Year = 2005},

new Book {ID = 7, Author = "Bob", Category = ".NET",

Name = "CLR Inside Outside", Publisher = "ORA",

Year = 2005},

new Book {ID = 8, Author = "Ibrahim", Category = "Inegration",

Name = "BIZTalk PipeLines", Publisher = "DHC",

Year = 2005},

new Book {ID = 9, Author = "Adam", Category = ".NET",

Name = "MSIL For Dummies", Publisher = "DHC",

Year = 2006},

new Book {ID = 10, Author = "Salim", Category = ".NET",

Name = "CLR Deep Dive", Publisher = "DN",

Year = 2006},

new Book {ID = 11, Author = "Hikmet", Category = "Windows",

Name = "Exchange Migration", Publisher = "MS",

Year = 2007},

new Book {ID = 12, Author = "Muhanad", Category = "SharePoint",

Name = "WSS Solutions", Publisher = "MS",

Year = 2007},

};

return BooksList;

}

static void Main(string[] args)

{

Page 19: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

19

//Enforcing the query immediate execution

// Using ToList Query Conversion Operator

List<Book> books = GetBooksList();

var bks =

(from b in books

where b.Category == ".NET"

select new {Category = b.Category.ToLower(),

Name = b.Name.ToUpper(),

Author = b.Author.ToUpper()}).ToList();

Console.WriteLine("Books Information");

Console.WriteLine("=================\n");

foreach (var bk in bks)

{

Console.WriteLine("Book Name :{0}", bk.Name);

Console.WriteLine("Book Catregory:{0}", bk.Category);

Console.WriteLine("Book Author :{0}", bk.Author);

Console.WriteLine("\n\n");

}

Console.ReadKey();

//Using ToArray Query Convesion Operator

string[] Authors =

(from b in books

select b.Author).Distinct().ToArray();

Console.WriteLine("Authors List");

Console.WriteLine("=================\n");

foreach (var auth in Authors)

{

Console.WriteLine(auth);

}

Console.ReadKey();

//Using ToDictionary Query Conversion Operator

var bkDic =

(from b in books

select new { b.ID, b.Name }).ToDictionary(bk => bk.ID);

Console.WriteLine("\n\n\nBooks Dictionary");

Console.WriteLine("================\n");

for (int i = 1; i < 13; i++)

{

Console.WriteLine("Book ID :{0}", i);

Console.WriteLine("Book Name:{0}", bkDic[i].Name);

}

Console.ReadKey();

//Using OfType Query Operator

object[] misc = {"CLR Via C#", bkDic[2], bkDic[1], Authors[1],

Authors[2], 12,43, books[0].Name, books[1],

233, "GeeksConnected", books[2].ID};

var strs =

(from s in misc

select s).OfType<string>();

Page 20: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

20

Console.WriteLine("Strings List");

Console.WriteLine("============\n");

foreach (var s in strs)

{

Console.WriteLine(s);

}

Console.ReadKey();

//Retrieving the Integers.....

var nums =

(from n in misc

select n).OfType<int>();

Console.WriteLine("Numeric List");

Console.WriteLine("============\n");

foreach (var n in nums)

{

Console.WriteLine(n);

}

Console.ReadKey();

}

}

}

6. Click Start on the Debug menu or press F5 to run the code.

Page 21: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

21

Partitioning Operators

Linq extension methods set contain a group of operators that allows the programmer to

partitioning the results of the query into a specific sequence, such as taking the first n number

of elements in the sequence, skipping n number of elements, and retrieving or skipping a

sequence of values based in specific criteria.

The following table contains the partitioning operators and a description about it.

No. Query Operator Description

1. Take Returns a specific number of continuous elements from the start of a

sequence of values.

2. Skip Bypasses specific number of elements from a sequence of values and

returning the remaining elements.

3. TakeWhile Returns a specific number of elements while a specific condition is

true and returning the remaining elements.

4. SkipWhile Bypasses elements from a sequence of values while a specific

condition is true and returning the remaining elements.

Page 22: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

22

Lab 4: Using Partitioning Operators

Objectives:

This lab introduces you the new generic collection type HashSet and its standard operations.

After completing this lab, you will be able to:

Use the partitioning query operators.

Skipping elements based on conditional expression.

Retrieving elements using Take and TakeWhile operators.

Using Partitioning Operators

7. On the File menu in Visual Studio 2008, point to New and then click Project.

8. In the New Project dialog box, select a language, and then select Windows in the

Project Types box.

9. In the Templates box, select Console Application.

10. In the Location box, type the path to where to create the application, and then click OK.

11. In the Visual Studio code editor as the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

Page 23: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

23

namespace GeeksConnected.Module03Lab02

{

class Book

{

public long ID { get; set; }

public string Name { get; set; }

public string Author { get; set; }

public string Publisher { get; set; }

public int Year { get; set; }

public string Category { get; set; }

public Book()

{

}

}

class Program

{

public static List<Book> GetBooksList()

{

List<Book> BooksList = new List<Book>

{

new Book {ID = 1, Author = "Muhanad", Category = "SharePoint",

Name = "Inside MOSS Customization", Publisher = "GC",

Year = 2008},

new Book {ID = 2, Author = "Mohamed", Category = ".NET",

Name = "Advanced .NET", Publisher = "GC",

Year = 2008},

new Book {ID = 3, Author = "Amjad", Category = "Integration",

Name = "Advanced BizTalk", Publisher = "GC",

Year = 2008},

new Book {ID = 4, Author = "Hikmet", Category = "Windows",

Name = "Windows 2008 Server", Publisher = "GC",

Year = 2008},

new Book {ID = 5, Author = "Ayman", Category = "Inegration",

Name = "BIZTalk Administration", Publisher = "DN",

Year = 2006},

new Book {ID = 6, Author = "Ayman", Category = "SharePoint",

Name = "Programming CAML", Publisher = "DN",

Year = 2005},

new Book {ID = 7, Author = "Bob", Category = ".NET",

Name = "CLR Inside Outside", Publisher = "ORA",

Year = 2005},

new Book {ID = 8, Author = "Ibrahim", Category = "Inegration",

Name = "BIZTalk PipeLines", Publisher = "DHC",

Year = 2005},

new Book {ID = 9, Author = "Adam", Category = ".NET",

Name = "MSIL For Dummies", Publisher = "DHC",

Year = 2006},

new Book {ID = 10, Author = "Salim", Category = ".NET",

Name = "CLR Deep Dive", Publisher = "DN",

Year = 2006},

new Book {ID = 11, Author = "Hikmet", Category = "Windows",

Name = "Exchange Migration", Publisher = "MS",

Year = 2007},

new Book {ID = 12, Author = "Muhanad", Category = "SharePoint",

Name = "WSS Solutions", Publisher = "MS",

Year = 2007},

};

return BooksList;

}

static void Main(string[] args)

{

Page 24: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

24

List<Book> books = GetBooksList();

//Using Take Operator

var bks =

(from b in books

where b.Category == ".NET"

select b

).Take(3);

Console.WriteLine(".NET Books");

Console.WriteLine("==========\n");

foreach (var bk in bks)

{

Console.WriteLine("Book Name :{0}", bk.Name);

Console.WriteLine("Book Catregory:{0}", bk.Category);

Console.WriteLine("Book Author :{0}", bk.Author);

Console.WriteLine("\n\n");

}

Console.ReadKey();

//Using TakeWhile Operator

var bksLst =

(from b in books

select b

).TakeWhile(b => b.Year == 2008);

Console.WriteLine("2008 Year Books");

Console.WriteLine("===================\n");

foreach (var bk in bksLst)

{

Console.WriteLine("Book Name :{0}", bk.Name);

Console.WriteLine("Book Catregory:{0}", bk.Category);

Console.WriteLine("Book Author :{0}", bk.Author);

Console.WriteLine("Book Year :{0}", bk.Year);

Console.WriteLine("\n\n");

}

Console.ReadKey();

//Using SkipWhile Operator

var bksList =

(from b in books

select b

).SkipWhile(b => b.Year == 2008);

Console.WriteLine("Books Information");

Console.WriteLine("=================\n");

foreach (var bk in bksList)

{

Console.WriteLine("Book Name :{0}", bk.Name);

Console.WriteLine("Book Catregory:{0}", bk.Category);

Console.WriteLine("Book Author :{0}", bk.Author);

Console.WriteLine("Book Year :{0}", bk.Year);

Console.WriteLine("\n");

}

Console.ReadKey();

Page 25: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

25

}

}

}

12. Click Start on the Debug menu or press F5 to run the code.

Page 26: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

26

LINQ Bridge

Linq requires the developer to target the .NET Framework 3.5 while developing any Linq-

Enabled Application. All the query operators are implemented in .NET Framework 3.5 which

forces the customer to install the .NET Framework 3.5.

Joseph & Ben Albahari write a reimplementation of all standard query operators which allows

the developers to write Linq to Objects applications into .NET Framework 2.0 by using the

Multi-Targeting Feature of Visual Studio 2008.

LinqBirdge is a free open source project you can download it under the following link:

http://www.albahari.com/nutshell/LinqBridge.zip

And the source code is available under the following link:

http://www.albahari.com/nutshell/LinqBridge.zip

LinqBridge Internals:

The C# 3.0 and .Net Framework 3.5 are designed to work with CLR 2.0 which means that the

generated IL instructions are the same instructions that used in .NET Framework 2.0

Applications.

The compiler looks for the query operators as extension methods which is used as static

methods in the generated IL instructions, the LinqBridge implementation contains the same

query operators implementation which enable the code to target .NET Framework 2.0.

Page 27: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

27

Lab 5: Using Linq for .NET Framework 2.0

After completing this lab, you will be able to:

Writing Linq-Enabled Applications for .Net Framework 2.0.

Using the LinqBridge extension.

Writing Linq to Objects expressions.

Using LinqBridge Extension

1. On the File menu in Visual Studio 2008, point to New and then click Project.

2. In the New Project dialog box, select a language, and then select Windows in the

Project Types box.

3. Choose the .Net Framework 2.0 in the Target Framework ListBox

4. In the Templates box, select Console Application.

5. In the Location box, type the path to where to create the application, and then click OK.

6. Add Reference to LinqBridge.dll assembly.

7. In the Visual Studio code editor as the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace GeeksConnected.Module03Lab02

{

class Book

{

Page 28: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

28

public long ID { get; set; }

public string Name { get; set; }

public string Author { get; set; }

public string Publisher { get; set; }

public int Year { get; set; }

public string Category { get; set; }

public Book()

{

}

}

class Program

{

public static List<Book> GetBooksList()

{

List<Book> BooksList = new List<Book>

{

new Book {ID = 1, Author = "Muhanad", Category = "SharePoint",

Name = "Inside MOSS Customization", Publisher = "GC",

Year = 2008},

new Book {ID = 2, Author = "Mohamed", Category = ".NET",

Name = "Advanced .NET", Publisher = "GC",

Year = 2008},

new Book {ID = 3, Author = "Amjad", Category = "Integration",

Name = "Advanced BizTalk", Publisher = "GC",

Year = 2008},

new Book {ID = 4, Author = "Hikmet", Category = "Windows",

Name = "Windows 2008 Server", Publisher = "GC",

Year = 2008},

new Book {ID = 5, Author = "Ayman", Category = "Inegration",

Name = "BIZTalk Administration", Publisher = "DN",

Year = 2006},

new Book {ID = 6, Author = "Ayman", Category = "SharePoint",

Name = "Programming CAML", Publisher = "DN",

Year = 2005},

new Book {ID = 7, Author = "Bob", Category = ".NET",

Name = "CLR Inside Outside", Publisher = "ORA",

Year = 2005},

new Book {ID = 8, Author = "Ibrahim", Category = "Inegration",

Name = "BIZTalk PipeLines", Publisher = "DHC",

Year = 2005},

new Book {ID = 9, Author = "Adam", Category = ".NET",

Name = "MSIL For Dummies", Publisher = "DHC",

Year = 2006},

new Book {ID = 10, Author = "Salim", Category = ".NET",

Name = "CLR Deep Dive", Publisher = "DN",

Year = 2006},

new Book {ID = 11, Author = "Hikmet", Category = "Windows",

Name = "Exchange Migration", Publisher = "MS",

Year = 2007},

new Book {ID = 12, Author = "Muhanad", Category = "SharePoint",

Name = "WSS Solutions", Publisher = "MS",

Year = 2007},

};

return BooksList;

}

static void Main(string[] args)

{

List<Book> books = GetBooksList();

var bks =

Page 29: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

29

from b in books

where b.Category == ".NET"

select b;

Console.WriteLine(".NET Books");

Console.WriteLine("==========\n");

foreach (var bk in bks)

{

Console.WriteLine("Book Name :{0}", bk.Name);

Console.WriteLine("Book Catregory:{0}", bk.Category);

Console.WriteLine("Book Author :{0}", bk.Author);

Console.WriteLine("\n\n");

}

Console.ReadKey();

}

}

}

8. Click Start on the Debug menu or press F5 to run the code.

Page 30: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

30

Summary

In this module, you learned how Linq use the new C# 3.0 enhancements together to enable

the query expressions, and you learned how to use the query operators into different query

manners.

This module focus in writing and reading Linq code and querying objects and in-memory data

which allows you to move to the next step in using different Linq implementation such as

Linq to XML, and Linq to SQL.

Here is a summary of what you’ve introduced in this module:

Writing Query Expressions.

Writing Queries using extension methods directly.

Using different query operators.

Enabling Linq to Objects into .NET Framework 2.0 Applications.

Page 31: Module 3: Introduction to LINQ (Material)

MODULE 3: INTRODUCTION TO LINQ

31

References

1. Microsoft Site (http://www.microsoft.com)

2. Microsoft Developer Network (http://msdn.microsoft.com)

3. Scott Guthrie’s Blog (http://weblogs.asp.net/scottgu/)

4. Scott Hanselman’s Blog(http://www.hanselman.com/)

5. Microsoft Developers Evangelists VS 2008 Training Kit.

6. MSDN 101 Linq Samples.