Module 3: Introduction to LINQ (Material)

2,291 views

Published on

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

Published in: Technology
  • Be the first to comment

Module 3: Introduction to LINQ (Material)

  1. 1. Visual Studio 2008 Community Training By Mohamed Saleh mohamedsaleh@live.com www.jordev.net www.geeksconnected.com/mohamed
  2. 2. MODULE 3: INTRODUCTION TO LINQMODULE 3: INTRODUCTION TOLINQ 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 312
  3. 3. MODULE 3: INTRODUCTION TO LINQOfficial Community Material LicenseWhile every precaution has been taken in the preparation of this document, Jordev assumes no responsibility forerrors, 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 thedocument, in its electronic or printed formats, be lost, deleted, destroyed or otherwise discarded, this disclaimerstatement 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.3
  4. 4. MODULE 3: INTRODUCTION TO LINQModule 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.4
  5. 5. MODULE 3: INTRODUCTION TO LINQIntroducing LINQ ProjectOverview:In the FoxPro/dBase days the data manipulation operations and queries was integrated withthe programming language itself, and it was supported natively in the language, then theClient/Server computing separates the data and the programming language, and the objectoriented technology become very mature and richer than before and empowered very muchfrom the programming language itself.The complexity of programming happens when dealing with data, and especially these dayswe 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 writeSQL-Like query expressions that can execute many operations over much kind of data whileusing the object oriented programming style.LINQ Building Blocks:The LINQ expressions are enabled because of the marriage between different new features inthe C# 3.0 Language which includes the following: 1. Object Initializers 2. Lambda Expressions 3. Extension Methods 4. Anonymous types 5. Implicit Typing VariablesAs we mentioned in the C# 3.0 module, the C# 3.0 features are only related to the compilerand it will not reflect any new IL instructions.All of these new features considered as the building blocks of the LINQ, which combinesthem together and enable the LINQ.5
  6. 6. MODULE 3: INTRODUCTION TO LINQLINQ AssembliesThe following table contains the LINQ assemblies with the main namespaces:No. Assembly Name Namespaces1. System.Core.dll System System.Linq System.Linq.Expressions2. System.Data.DataSetExtensions.dll System.Data3. System.Data.Linq.dll System.Data.Linq System.Data.Linq.Mapping System.Data.Linq.SqlClient4. System.Xml.Linq.dll System.Xml.Linq System.Xml.Schema System.Xml.XPathThe first assembly System.Core provides extensions methods for IEnumerable<T> andsome 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 forDataSet.The System.Data.Linq assembly is the implementation of LINQ for SQL Server databaseswhich contains many classes that represent the tables as entities and provides some attributesfor 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 theprogrammer to construct Xml documents using LINQ expressions and query it such as anyother data.6
  7. 7. MODULE 3: INTRODUCTION TO LINQLINQ Query ExpressionsOverview:Query expressions can be written using declarative query syntax enabled by the C# 3.0language 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-classlanguage construct. A query expression consists of a set of clauses written in a declarativesyntax 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 queryexpression. (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;7
  8. 8. MODULE 3: INTRODUCTION TO LINQThe names represents the query variable that store the query, the query expression must beginwith from clause and must end with select/group clause. Between the from and select clauseit can contains optional clauses such as where, join, or orderby.Query Operators:Query operators are static extension methods that construct the query expressions, thefollowing statements shows how to write query expressions using the extension methodsdirectly: 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 aremany different query operators shipped with the Linq, in this module you will do samplesover 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 thatimplements 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 operatorsand can be done lately - which is the default case - which called deferred query execution orthe lazy execution. The deferred query execution enables the reusability of query even if thedata changed.8
  9. 9. MODULE 3: INTRODUCTION TO LINQLab 1: Writing Query ExpressionsAfter 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 {9
  10. 10. MODULE 3: INTRODUCTION TO LINQ 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 List10
  11. 11. MODULE 3: INTRODUCTION TO LINQ 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.11
  12. 12. MODULE 3: INTRODUCTION TO LINQStandard Query OperatorsEnumerable Extension Methods:Linq shipped with a lot of extension methods which act as the query operators when writingquery expressions.The System.Linq namespace contains different Linq Extensions under theSystem.Linq.Enumerable class, the extension methods can be consumed through the usingdirective, and by default the projects in .NET Framework 3.5 are using the System.Linqnamespace.The following table contains the some commonly used operators:No. Extension Method Description1. 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.12
  13. 13. MODULE 3: INTRODUCTION TO LINQLab 2: Writing Queries Using ExtensionMethodsAfter 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 methods1. 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; }13
  14. 14. MODULE 3: INTRODUCTION TO LINQ 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);14
  15. 15. MODULE 3: INTRODUCTION TO LINQ 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("nnFirst 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("nnDistinct 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.15
  16. 16. MODULE 3: INTRODUCTION TO LINQConversion OperatorsLinq can convert a query results to different data collections types, as we mentioned beforethe default execution behavior of the query is the deferred execution, which execute the queryand 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 animmediate execution to the query.No. Query Operator Description1. 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.16
  17. 17. MODULE 3: INTRODUCTION TO LINQLab 3: Using Conversion OperatorsObjectives:This lab introduces you the query conversion operators which is a set of extension methods onthe 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 Operators1. 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;17
  18. 18. MODULE 3: INTRODUCTION TO LINQnamespace 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) {18
  19. 19. MODULE 3: INTRODUCTION TO LINQ //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("nn"); } 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("nnnBooks 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>();19
  20. 20. MODULE 3: INTRODUCTION TO LINQ 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.20
  21. 21. MODULE 3: INTRODUCTION TO LINQPartitioning OperatorsLinq extension methods set contain a group of operators that allows the programmer topartitioning the results of the query into a specific sequence, such as taking the first n numberof elements in the sequence, skipping n number of elements, and retrieving or skipping asequence of values based in specific criteria.The following table contains the partitioning operators and a description about it.No. Query Operator Description1. 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.21
  22. 22. MODULE 3: INTRODUCTION TO LINQLab 4: Using Partitioning OperatorsObjectives: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 Operators7. 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;22
  23. 23. MODULE 3: INTRODUCTION TO LINQnamespace 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) {23
  24. 24. MODULE 3: INTRODUCTION TO LINQ 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("nn"); } 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("nn"); } 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();24
  25. 25. MODULE 3: INTRODUCTION TO LINQ } }}12. Click Start on the Debug menu or press F5 to run the code.25
  26. 26. MODULE 3: INTRODUCTION TO LINQLINQ BridgeLinq 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 whichforces the customer to install the .NET Framework 3.5.Joseph & Ben Albahari write a reimplementation of all standard query operators which allowsthe developers to write Linq to Objects applications into .NET Framework 2.0 by using theMulti-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.zipAnd the source code is available under the following link:http://www.albahari.com/nutshell/LinqBridge.zipLinqBridge Internals:The C# 3.0 and .Net Framework 3.5 are designed to work with CLR 2.0 which means that thegenerated IL instructions are the same instructions that used in .NET Framework 2.0Applications.The compiler looks for the query operators as extension methods which is used as staticmethods in the generated IL instructions, the LinqBridge implementation contains the samequery operators implementation which enable the code to target .NET Framework 2.0.26
  27. 27. MODULE 3: INTRODUCTION TO LINQLab 5: Using Linq for .NET Framework 2.0After 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 Extension1. 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 ListBox4. 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 {27
  28. 28. MODULE 3: INTRODUCTION TO LINQ 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 =28
  29. 29. MODULE 3: INTRODUCTION TO LINQ 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("nn"); } Console.ReadKey(); } }}8. Click Start on the Debug menu or press F5 to run the code.29
  30. 30. MODULE 3: INTRODUCTION TO LINQSummaryIn this module, you learned how Linq use the new C# 3.0 enhancements together to enablethe query expressions, and you learned how to use the query operators into different querymanners.This module focus in writing and reading Linq code and querying objects and in-memory datawhich allows you to move to the next step in using different Linq implementation such asLinq 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.30
  31. 31. MODULE 3: INTRODUCTION TO LINQReferences1. 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.31

×