SlideShare a Scribd company logo
Visual Studio 2008
  Community Training




              By
         Mohamed Saleh
        mohamedsaleh@live.com
            www.jordev.net
    www.geeksconnected.com/mohamed
MODULE 3: INTRODUCTION TO LINQ




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




2
MODULE 3: INTRODUCTION TO LINQ




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.




3
MODULE 3: INTRODUCTION TO LINQ




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.




4
MODULE 3: INTRODUCTION TO LINQ




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.


5
MODULE 3: INTRODUCTION TO LINQ




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.



6
MODULE 3: INTRODUCTION TO LINQ




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;




7
MODULE 3: INTRODUCTION TO LINQ




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.




8
MODULE 3: INTRODUCTION TO LINQ




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
        {




9
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 List



10
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
MODULE 3: INTRODUCTION TO LINQ




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.




12
MODULE 3: INTRODUCTION TO LINQ




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; }



13
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
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
MODULE 3: INTRODUCTION TO LINQ




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.




16
MODULE 3: INTRODUCTION TO LINQ




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;




17
MODULE 3: INTRODUCTION TO LINQ



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)
         {



18
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
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
MODULE 3: INTRODUCTION TO LINQ




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.




21
MODULE 3: INTRODUCTION TO LINQ




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;




22
MODULE 3: INTRODUCTION TO LINQ



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)
         {



23
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
MODULE 3: INTRODUCTION TO LINQ




         }
     }
}



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




25
MODULE 3: INTRODUCTION TO LINQ




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.




26
MODULE 3: INTRODUCTION TO LINQ




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
    {



27
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
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
MODULE 3: INTRODUCTION TO LINQ




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.




30
MODULE 3: INTRODUCTION TO LINQ




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.




31

More Related Content

What's hot

Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
Satyamevjayte Haxor
 
COM Introduction
COM IntroductionCOM Introduction
COM Introduction
Roy Antony Arnold G
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Akhil Mittal
 
Rein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4jRein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4j
Razorsight
 
C programming session 14
C programming session 14C programming session 14
C programming session 14
Vivek Singh
 
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Coen De Roover
 
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Shinpei Hayashi
 
Ppt chapter07
Ppt chapter07Ppt chapter07
Ppt chapter07
Richard Styner
 
Aspect-Oriented Programming and Depedency Injection
Aspect-Oriented Programming and Depedency InjectionAspect-Oriented Programming and Depedency Injection
Aspect-Oriented Programming and Depedency Injection
Robert Lemke
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Raffi Khatchadourian
 
Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...
Shinpei Hayashi
 
Application package
Application packageApplication package
Application package
JAYAARC
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
Richard Styner
 
Software architacture recovery
Software architacture recoverySoftware architacture recovery
Software architacture recovery
Imdad Ul Haq
 
Refactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_WorkshopRefactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_Workshop
Max Kleiner
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
Deepmala Sharma
 
C programming session 14
C programming session 14C programming session 14
C programming session 14
AjayBahoriya
 
JaCIL_ a CLI to JVM Compiler
JaCIL_ a CLI to JVM CompilerJaCIL_ a CLI to JVM Compiler
JaCIL_ a CLI to JVM Compiler
Almann Goo
 

What's hot (18)

Linking in MS-Dos System
Linking in MS-Dos SystemLinking in MS-Dos System
Linking in MS-Dos System
 
COM Introduction
COM IntroductionCOM Introduction
COM Introduction
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
Rein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4jRein_in_the_ability_of_log4j
Rein_in_the_ability_of_log4j
 
C programming session 14
C programming session 14C programming session 14
C programming session 14
 
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
 
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
Recording Finer-Grained Software Evolution with IDE: An Annotation-Based Appr...
 
Ppt chapter07
Ppt chapter07Ppt chapter07
Ppt chapter07
 
Aspect-Oriented Programming and Depedency Injection
Aspect-Oriented Programming and Depedency InjectionAspect-Oriented Programming and Depedency Injection
Aspect-Oriented Programming and Depedency Injection
 
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMUAutomated Refactoring of Legacy Java Software to Default Methods Talk at GMU
Automated Refactoring of Legacy Java Software to Default Methods Talk at GMU
 
Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...Toward Understanding How Developers Recognize Features in Source Code from De...
Toward Understanding How Developers Recognize Features in Source Code from De...
 
Application package
Application packageApplication package
Application package
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
 
Software architacture recovery
Software architacture recoverySoftware architacture recovery
Software architacture recovery
 
Refactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_WorkshopRefactoring_Rosenheim_2008_Workshop
Refactoring_Rosenheim_2008_Workshop
 
Unit 4 sp macro
Unit 4 sp macroUnit 4 sp macro
Unit 4 sp macro
 
C programming session 14
C programming session 14C programming session 14
C programming session 14
 
JaCIL_ a CLI to JVM Compiler
JaCIL_ a CLI to JVM CompilerJaCIL_ a CLI to JVM Compiler
JaCIL_ a CLI to JVM Compiler
 

Viewers also liked

November
NovemberNovember
November
St. Mary School
 
Oct 2
Oct 2Oct 2
E Learning Catalog - Servers, VS 2005, etc
E Learning Catalog  - Servers, VS 2005, etcE Learning Catalog  - Servers, VS 2005, etc
E Learning Catalog - Servers, VS 2005, etc
prasad_lk
 
Predictably Irrational Customers
Predictably Irrational CustomersPredictably Irrational Customers
Predictably Irrational Customers
Eric Siegmann
 
Facebook Presentation
Facebook  PresentationFacebook  Presentation
Facebook Presentation
melover123
 
Obamas Mother
Obamas MotherObamas Mother
Obamas Mother
guest262cdb
 
Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)
Mohamed Saleh
 
Module 4: Introduction to ASP.NET 3.5 (Material)
Module 4: Introduction to ASP.NET 3.5 (Material)Module 4: Introduction to ASP.NET 3.5 (Material)
Module 4: Introduction to ASP.NET 3.5 (Material)
Mohamed Saleh
 

Viewers also liked (9)

Prezentacia
PrezentaciaPrezentacia
Prezentacia
 
November
NovemberNovember
November
 
Oct 2
Oct 2Oct 2
Oct 2
 
E Learning Catalog - Servers, VS 2005, etc
E Learning Catalog  - Servers, VS 2005, etcE Learning Catalog  - Servers, VS 2005, etc
E Learning Catalog - Servers, VS 2005, etc
 
Predictably Irrational Customers
Predictably Irrational CustomersPredictably Irrational Customers
Predictably Irrational Customers
 
Facebook Presentation
Facebook  PresentationFacebook  Presentation
Facebook Presentation
 
Obamas Mother
Obamas MotherObamas Mother
Obamas Mother
 
Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)Module 2: C# 3.0 Language Enhancements (Slides)
Module 2: C# 3.0 Language Enhancements (Slides)
 
Module 4: Introduction to ASP.NET 3.5 (Material)
Module 4: Introduction to ASP.NET 3.5 (Material)Module 4: Introduction to ASP.NET 3.5 (Material)
Module 4: Introduction to ASP.NET 3.5 (Material)
 

Similar to Module 3: Introduction to LINQ (Material)

Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)
Mohamed Saleh
 
Linq in C#
Linq in C#Linq in C#
Linq in C#
Umar Farooq
 
LINQ
LINQLINQ
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
cNguyn506241
 
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptxSQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
antony194610
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5
Peter Gfader
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
Akhil Mittal
 
Module 2: C# 3.0 Language Enhancements (Material)
Module 2: C# 3.0 Language Enhancements (Material)Module 2: C# 3.0 Language Enhancements (Material)
Module 2: C# 3.0 Language Enhancements (Material)
Mohamed Saleh
 
Current & Future Use-Cases of OpenDaylight
Current & Future Use-Cases of OpenDaylightCurrent & Future Use-Cases of OpenDaylight
Current & Future Use-Cases of OpenDaylight
abhijit2511
 
Cognos Software Development Kit
Cognos Software Development KitCognos Software Development Kit
Linq
LinqLinq
Project_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_finalProject_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_final
Jerin John
 
C# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy today
Afonso Macedo
 
Portfolio
PortfolioPortfolio
Portfolio
jeanux
 
C# Unit 1 notes
C# Unit 1 notesC# Unit 1 notes
C# Unit 1 notes
Sudarshan Dhondaley
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
C# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkC# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech Talk
Michael Heydt
 
Net Fundamentals
Net FundamentalsNet Fundamentals
Net Fundamentals
Ali Taki
 
Summarization Techniques for Code, Changes, and Testing
Summarization Techniques for Code, Changes, and TestingSummarization Techniques for Code, Changes, and Testing
Summarization Techniques for Code, Changes, and Testing
Sebastiano Panichella
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
Nico Ludwig
 

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

Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)Module 3: Introduction to LINQ (PowerPoint Slides)
Module 3: Introduction to LINQ (PowerPoint Slides)
 
Linq in C#
Linq in C#Linq in C#
Linq in C#
 
LINQ
LINQLINQ
LINQ
 
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
 
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptxSQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
SQL ttrain wrwrwrw wwrw wwrrrwrwrwrwwrwr.pptx
 
C# advanced topics and future - C#5
C# advanced topics and future - C#5C# advanced topics and future - C#5
C# advanced topics and future - C#5
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 
Module 2: C# 3.0 Language Enhancements (Material)
Module 2: C# 3.0 Language Enhancements (Material)Module 2: C# 3.0 Language Enhancements (Material)
Module 2: C# 3.0 Language Enhancements (Material)
 
Current & Future Use-Cases of OpenDaylight
Current & Future Use-Cases of OpenDaylightCurrent & Future Use-Cases of OpenDaylight
Current & Future Use-Cases of OpenDaylight
 
Cognos Software Development Kit
Cognos Software Development KitCognos Software Development Kit
Cognos Software Development Kit
 
Linq
LinqLinq
Linq
 
Project_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_finalProject_Report (BARC-Jerin)_final
Project_Report (BARC-Jerin)_final
 
C# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy todayC# c# for beginners crash course master c# programming fast and easy today
C# c# for beginners crash course master c# programming fast and easy today
 
Portfolio
PortfolioPortfolio
Portfolio
 
C# Unit 1 notes
C# Unit 1 notesC# Unit 1 notes
C# Unit 1 notes
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
C# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech TalkC# 3.0 and LINQ Tech Talk
C# 3.0 and LINQ Tech Talk
 
Net Fundamentals
Net FundamentalsNet Fundamentals
Net Fundamentals
 
Summarization Techniques for Code, Changes, and Testing
Summarization Techniques for Code, Changes, and TestingSummarization Techniques for Code, Changes, and Testing
Summarization Techniques for Code, Changes, and Testing
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 

Recently uploaded

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 

Recently uploaded (20)

RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 

Module 3: Introduction to LINQ (Material)

  • 1. Visual Studio 2008 Community Training By Mohamed Saleh mohamedsaleh@live.com www.jordev.net www.geeksconnected.com/mohamed
  • 2. MODULE 3: INTRODUCTION TO LINQ 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 2
  • 3. MODULE 3: INTRODUCTION TO LINQ 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. 3
  • 4. MODULE 3: INTRODUCTION TO LINQ 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. 4
  • 5. MODULE 3: INTRODUCTION TO LINQ 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. 5
  • 6. MODULE 3: INTRODUCTION TO LINQ 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. 6
  • 7. MODULE 3: INTRODUCTION TO LINQ 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; 7
  • 8. MODULE 3: INTRODUCTION TO LINQ 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. 8
  • 9. MODULE 3: INTRODUCTION TO LINQ 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 { 9
  • 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 List 10
  • 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. MODULE 3: INTRODUCTION TO LINQ 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. 12
  • 13. MODULE 3: INTRODUCTION TO LINQ 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; } 13
  • 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. 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. MODULE 3: INTRODUCTION TO LINQ 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. 16
  • 17. MODULE 3: INTRODUCTION TO LINQ 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; 17
  • 18. MODULE 3: INTRODUCTION TO LINQ 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) { 18
  • 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. 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. MODULE 3: INTRODUCTION TO LINQ 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. 21
  • 22. MODULE 3: INTRODUCTION TO LINQ 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; 22
  • 23. MODULE 3: INTRODUCTION TO LINQ 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) { 23
  • 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. MODULE 3: INTRODUCTION TO LINQ } } } 12. Click Start on the Debug menu or press F5 to run the code. 25
  • 26. MODULE 3: INTRODUCTION TO LINQ 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. 26
  • 27. MODULE 3: INTRODUCTION TO LINQ 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 { 27
  • 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. 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. MODULE 3: INTRODUCTION TO LINQ 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. 30
  • 31. MODULE 3: INTRODUCTION TO LINQ 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. 31