Linq-rev2 - Western Michigan University

Transcription

IntroductionI t d ti ToT LINQThis material is based on the original slides of John Hidey, Mike Bird, Sharp Dudes, LLCData AccessData ! Objects(Object Relational Mapping)Build database-like search functionality into they data: object,j ,.NET Framework that works on anyXML, SQL, custom types LINQ: Language INtegrated Query

C# 3.0 Language InnovationsLocal variablettype inferencei fvar contacts Queryfrom c in customersexpressionswhere c.State "WA"select new { c.Name, c.Phone };Lambdaexpressionsvar contactst t customers.Where(c( c.State "WA")).Select(c new { c.Name, c.Phone sLINQQ To ObjectsjRestrictionWhereProjectionSelect, SelectManyOrderingOrderBy, ThenByGroupingGroupByJoinsJoin, GroupJoinQuantifiersAny, AllPartitioningTake Skip,Take,Skip TakeWhile,TakeWhile SkipWhileSetsDistinct, Union, Intersect, ExceptElementsFirst, Last, Single, ElementAtAggregationCount, Sum, Min, Max, AverageConversionToArray, ToList, ToDictionaryCastingOfType T , Cast T

LINQ Basics Query Operators can be used against any .NET collection(IEnumerable T ) Built in examples: SelectBuilt-inSelect, WhereWhere, GroupByGroupBy, Join,Join etc.etcExtensibility model supports adding/replacing them Query Expressions can operate on information sourcesand apply query operators against them to returnIEnumerable T sequencesLINQ to SQL Language integrated data access Maps tables and rows to classes and objectsBuilds on ADOADO.NETNET and .NETNET Transactions Mapping Encoded in attributes or external XML fileRelationships map to properties Persistence Automatic change trackingUpdates through SQL or stored procedures

LINQ to SQLAAccessingi datad t todayt dSqlConnection c new SqlConnection( );c.Open();SqlCommandqcmd new SqlCommand(q(@"SELECT c.Name, c.PhoneFROM Customers cWHERE c.City @p0");cmd Parameters AddWithValue("@p0"cmd.Parameters.AddWithValue(@p0 , "London“);London );DataReader dr c.ExecuteReader(cmd);while (dr.Read()) {string name dr.GetString(0);string phone dr.GetString(1);DateTime date dr.GetDateTime(2);}dr Close();dr.Close();Queries inquotesLoosely boundargumentsLoosely typedresult setsNo compile timechecksLINQ to SQLAccessingAi datad t withith LINQpublic class Customer { }public class Northwind : DataContext{public Table Customer Customers; }Northwind db new Northwind( );var contacts from c in db.Customerswhere c.City "London"select new { c.Name, c.Phone };Classesdescribe dataTables are likecollectionsStrongly typedconnectionsI tIntegratedt dquery syntaxStronglyStl typedtdresults

LINQ to SQL nField / PropertyRelationshipField / PropertyStored ProcedProcedurereMethodLINQ to SQL Architecturefrom c in db.Customerswhere c.City "London"select c.CompanyNameLINQ y )LINQ to SQLSQL QueryRowsSELECT CompanyNameFROM CustWHERE City 'London'DML or SProcsINSERT INTO Cust UPDATE Cust DELETE FROM Cust SQL Server

Key Points FlexibleFl ibl mappingi “Classes first” or “data first”, attributes or mapping file DataContext Strongly typed database connection Entity classes Identity mapping and change tracking Relationships One-to-one, one-to-manyLINQ to SQL Access relational data as strongly typed objects LanguageLiintegratedtt d query Works with existing infrastructure Unified query and transform of objects, relational,XML

Resources The LINQ Project on MSDN 4594.aspx) Charlie Calvert’s Blog (http://blogs.msdn.com/charlie/) Mike Taulty’s Blog (http://mtaulty.com/) LINQ in Action (http://linqinaction.net/) Scott Gu’s Blog (http://weblogs.asp.net/scottgu/default.aspx)

Title: Microsoft PowerPoint - Linq-rev2 [Compatibility Mode] Author: ala_2 Created Date: 5/21/2008 3:59:05 PM