slide1
Download
Skip this Video
Download Presentation
LINQ

Loading in 2 Seconds...

play fullscreen
1 / 21

LINQ - PowerPoint PPT Presentation


  • 126 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'LINQ' - meara


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
slide1

LINQ

Joshua Clark

Consultant

SogetiUSA

Email: [email protected]

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

slide2

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

slide4

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

slide6

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
slide7

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);
  • }
slide8

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
slide11

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
slide12

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
slide13

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(“@City”, “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

slide15

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)

)

);

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

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

ad