LINQ
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

LINQ PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on
  • Presentation posted in: General

LINQ. Joshua Clark Consultant Sogeti USA Email: [email protected] Blog: http://joshua-clark.blogspot.com. About Me - C#/ASP.NET - SQL Server 2005 Reporting Services - SQL Server 2005 - LINQ to SQL Development - Web Services. What is LINQ? Quick LINQ Basics Overview

Download Presentation

LINQ

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Linq

LINQ

Joshua Clark

Consultant

SogetiUSA

Email: [email protected]

Blog: http://joshua-clark.blogspot.com


Linq

About Me - C#/ASP.NET - SQL Server 2005 Reporting Services - SQL Server 2005 - LINQ to SQL Development- Web Services


Agenda

  • What is LINQ?

  • Quick LINQ Basics Overview

  • LINQ to Objects

  • LINQ to SQL

  • Q & A

Agenda


Linq

What is LINQ?

  • Language Integrated Query

  • It’s a technology that bridges the gap between programming languages and data


The linq project

<book>

<title/>

<author/>

<year/>

<price/>

</book>

Relational

Objects

XML

The LINQ Project

.NET Language Integrated Query

C# 3.0

Visual Basic 9.0

Others

LINQ toObjects

LINQ toDataSets

LINQ toSQL

LINQ toEntities

LINQ toXML


Linq

What you need to LINQ

  • Visual C# 2008 Express Edition

  • Visual Basic 2008 Express Edition

  • Visual Web Developer 2008 Express

  • Visual Studio 2008 Std. or higher

  • LINQ to SQL

    • SQL Server 2005 Express

    • SQL Server 2000

    • SQL Server 2005

    • SQL Server 2008* - New data types not supported by first release of LINQ to SQL


Linq

  • LINQ to Objects

  • string[]mystuff =

  • {“linq”, “is”, “awesome”};

  • varwordswithl = from stuff in mystuff

  • wherestuff.Contains(“l”)

  • select stuff;

  • foreach(var n in wordswithl)

  • {

  • Console.WriteLine(n);

  • }


Linq

C# 3.0/VB 9 Language Enhancements

  • Implicitly Typed Variables

    • varmystring = “MyString”

  • Object Initializers

    • Customer c = new Customer() { Name = “Joe”, Address = “123 West St.”};

  • Lambda Expressions

    • (c => c.CompanyName == “Sogeti”)

    • delegate( Customer c) {return c.CompanyName == “Sogeti”}

  • Extension Methods

    • .Select(), .Where(), .Single(), .FirstOrDefault()

  • Anonymous Types

    • Types that are automatically generated for us by the compiler


Linq

LINQ to Objects

demo


Linq

LINQ to SQL – The Challenge

Data != Objects


Linq

LINQ to SQL

  • A new data access API that we can use to work with SQL Server

  • An object relational mapper that takes us from objects in memory to the database and back again

  • Several tools that we can use to help bridge the gap between our object model and our relational model

  • LINQ Pattern/Syntax put on top this new API


Linq

LINQ to SQL

  • Design Points

    • Rapid application development against SQL Server Database

      • Direct mapping to Microsoft SQL Server schema

      • Mappings expressed in attributes in XML file

    • 1-to-1 .NET Classes and SQL tables

    • Can bring in Stored Procedures, Table-valued functions , Views

    • Foreign keys are shown as relationships – designer builds the relationship for us automatically from database

  • Customization

    • Business Logic

      • Partial Classes for generated Objects – does not get wiped out when you re-generate your auto-generate code

    • Update Logic

      • Call SP to invoke custom logic


Linq

LINQ to SQL

Typical ADO.NET data-access code today…

using (SqlConnectionconn = new SqlConnection(“…”))

{

conn.Open();

SqlCommandcmd = conn.CreateCommand();

cmd = CommandText = “SELECT Name, Country

FROM Customers

WHERE City = @City”;

cmd.Parameters.AddWithValue([email protected], “Cleveland”);

using(SqlDataReaderrdr = cmd.ExecuteReader())

{

while(rdr.Read())

{

string name = rdr.GetString(0);

string country = rdr.GetString(1);

….

}

}

}

SQL Query is a string

Loosely bound parameters

Loosely type columns


Linq

LINQ to SQL

demo


Linq

Understanding Deferred Query Execution

  • Without lazy loading LINQ queries would perform poorly

  • Important thing to understand about LINQ queries is not their result is but what they can return

  • Allows us to create a query and reuse it at a later time

  • Forcing Immediate Execution…

    • In some instances, we may need to get all the data from a sequence and we do not want to iterate over every item

      • query.ToList(), query.ToArray()


  • Linq to xml

    • Must reference System.Xml.Linq

    • Constructing XML in a very straight-forward approach

    • Constructors help with creating XML

    LINQ to XML

    XElement contacts= new XElement(“contacts”,

    from c in customers

    where c.Country == “USA”

    select new XElement(“contact”,

    new Xelement(“name”, c.CompanyName),

    new Xelement(“phone” , c.Phone)

    )

    );


    Linq

    LINQ to XML

    demo


    Linqpad net

    • Pros

      • FREE tool to use to run LINQ queries

      • Expedites the process of shaping your data

      • Can connect to any database

    • Cons

      • No intellisense on LINQ syntax

      • Object names are case-sensitive

    LINQPad.NET


    L inqpad net

    Linqpad.net

    demo


    Summary

    • Data != Objects

    • Imperative => Declarative

    • Query against objects, databases, XML

    • Get LinqPad.net

    • More Information on LINQ

    • - http://msdn.microsoft.com/data/ref/linq

    • - http://linqinaction.net – Get the book!

    • - http://miketaulty.com/blog

    • - http://danielmoth.com/blog

    Summary


    References

    • “LINQ in Action”, FabriceMargurie, Steve Eichert, Jim Wooley

    • MSDN Webcasts series

    • Daniel Moth, Microsoft , LINQ screencasts

    • MikeTaulty, Microsoft , LINQ to SQL screencasts

    • Marc Schweigert, Microsoft , webcast

    References


  • Login