640 pro LINQ

746 666 0
  • Loading ...
1/746 trang
Tải xuống

Thông tin tài liệu

Ngày đăng: 11/07/2018, 14:07

CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Joseph C Rattz, Jr., author of Pro LINQ: Language Integrated Query in C# 2008 Pro LINQ: Language Integrated Query in VB 2008 Joseph C Rattz, Jr THE APRESS ROADMAP Pro VB 2008 and the NET 3.5 Platform Beginning VB 2008 Pro LINQ in VB 2008 Language Integrated Query in VB 2008 Pro LINQ: Language Integrated Query in VB 2008 is all about code Literally, this book starts with code and ends with code In writing this book, it has been my desire to create a treasury of meaningful LINQ examples Rather than show you a single, simplest case example, I’ve strived to fill in the whole picture and demonstrate the breadth of LINQ operators and declarations that are available to you With this information, you will be able to put LINQ to use as it was intended and reap the maximum rewards for your investment Throughout this book, it is my aim to give you the information that actually matters in a form that you can use So, rather than obscure the relevant LINQ principles by focusing on a complex demonstration application you can’t put to practical use, Pro LINQ cuts right to the chase of each LINQ operator, method, or class However, where complexity is necessary to truly demonstrate an issue, the examples are right there in the thick of it For example, code samples demonstrating how to handle concurrency conflicts actually create concurrency conflicts, so you can step through the code and see them unfold This book is for anyone with an elementary understanding of VB.NET who wants to understand LINQ and LINQ-relevant VB.NET 9.0 language features You need not be up on all the latest VB.NET 8.0 or 9.0 features to understand Pro LINQ When a deeper knowledge of an advanced language feature is necessary, I begin from the ground up to make sure everyone is well equipped for the discussion Pro LINQ Dear Reader, THE EXPERT’S VOICE ® IN NET Pro LINQ Language Integrated Query in VB 2008 Learn to use the power of Microsoft’s ground-breaking new technology Expert VB 2008 Business Objects Beginning VB 2008 Databases Pro WPF in VB 2008 SOURCE CODE ONLINE www.apress.com ISBN 978-1-4302-1644-5 54499 US $44.99 Rattz Hayes Joseph C Rattz, Jr and Dennis Hayes Shelve in Programming/VB 2008 User level: Intermediate–Advanced 781430 216445 www.it-ebooks.info this print for content only—size & color not accurate spine = 1.40625" 744 page count www.it-ebooks.info Pro LINQ Language Integrated Query in VB 2008 Joseph C Rattz, Jr and Dennis Hayes www.it-ebooks.info 16445fm_CMP2.indd 7/17/09 10:51:53 AM Pro LINQ: Language Integrated Query in VB 2008 Copyright © 2009 by Joseph C Rattz, Jr and Dennis Hayes All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher ISBN-13 (pbk): 978-1-4302-1644-5 ISBN-13 (electronic): 978-1-4302-1645-2 Printed and bound in the United States of America Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Lead Editor: Ewan Buckingham Technical Editor: Joseph C Rattz, Jr Technical Reviewers: Joseph C Rattz, Jr., Fabio Ferracchiati Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Richard Dal Porto Copy Editor: Heather Lang Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositors: Dina Quan, Patrick Cunningham Proofreader: April Eddy Indexer: Carol Burbo Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail kn`ano)juaran]cao6 -,.(,30*/As I mentioned, views are read-only In Listing 18-2, I will attempt to insert a record into a view Listing 18-2 Attempting to Insert a Record into a View, Which Will Not Succeed @ei`^=oJasJknpdsej`$[ @]p]Okqn_a9*XOMHATLNAOO7Ejepe]h?]p]hkc9Jknpdsej`7Ejpacn]pa`Oa_qnepu9OOLE7% `^*?]packnuO]haoBkn-553o*EjoanpKjOq^iep$[ Jas?]packnuO]haoBkn-553Sepdw[ *?]packnuJ]ia9Hacqiao([ *?]packnuO]hao9?@a_$35,0/*5.%y% www.it-ebooks.info 16445ch18.indd 682 7/17/09 2:56:49 PM C H A P T E R N A D D I T I O N A L S Q L C A P A B I LI T I E S 683 Notice that, in Listing 18-2, I not even bother to call the Oq^iep?d]jcao method This is because I know the code will not make it that far without an exception being thrown Let’s look at the results: Qjd]j`ha`At_alpekj6Ouopai*Ejr]he`Klan]pekjAt_alpekj6?]j#planbkni?na]pa(Ql`]pa kn@ahapaklan]pekjokj#P]^ha$?]packnuO]haoBkn-553%#^a_]qoaepd]ojklnei]nugau* Allow me to provide a warning though While the EjoanpKjOq^iep and @ahapaKjOq^iep methods will throw exceptions when called on a P]^ha$KbP% mapped to a database view, nothing will prevent you from making changes to a view’s entity object’s property You can change the property’s value and even call the Oq^iep?d]jcao method without an exception being thrown, but the change to the view’s entity object property will not be persisted to the database Entity Class Inheritance So far, in all my LINQ to SQL discussion excluding inheritance mapping in Chapter 15, there has been a single entity class mapped to a single table for any table that has an entity class mapped to it Thus, the mapping between entity classes and tables has been one-to-one so far NCaution The example used in this section creates a data model containing Omq]na and Na_p]jcha classes Geometrically speaking, a square is a rectangle, but a rectangle is not necessarily a square However, in the data model created for this example, the reverse relationship is true This class model defines a rectangle to be derived from a square Therefore, a rectangle is a square, but a square is not necessarily a rectangle The reasoning for this is explained in the text LINQ to SQL also offers an alternative to this, known as entity class inheritance Entity class inheritance allows a class hierarchy to be mapped to a single database table For that single database table, there must be a base entity class, and the appropriate entity class attribute mappings for the database table must be specified That base class will contain all properties common to every class in the hierarchy deriving from the base class, while the derived classes will only contain properties that are specific to that derived class, as is typical with any object model Here is an example of a base entity class without mapped derived classes: My Base Entity Class Without Mapped Derived Classes 8P]^ha:[ Lq^he_?h]ooOd]la 8?khqij$EoLnei]nuGau69Pnqa(Eo@^Cajan]pa`69Pnqa([ @^Pula69EjpJKPJQHHE@AJPEPU%:[ Lq^he_E`=oEjpacan www.it-ebooks.info 16445ch18.indd 683 7/17/09 2:56:49 PM 684 CH APT ER 18 N A DDITIONA L S QL C A P A B IL ITIE S 8?khqij$Eo@eo_neiej]pkn69Pnqa(@^Pula69JR]n?d]n$.%%:[ Lq^he_Od]la?k`a=oOpnejc 8?khqij$@^Pula69Ejp%:[ Lq^he_Op]npejcT=oEjpacan 8?khqij$@^Pula69Ejp%:[ Lq^he_Op]npejcU=oEjpacan Aj`?h]oo As you can see, I have specified the P]^ha attribute, and since no J]ia attribute property has been specified, the base entity class is mapped to table by the same name as the class, so it is mapped to the Od]la table Don’t worry that you not have a Od]la table at this time I will use the @]p]?kjpatp object’s ?na]pa@]p]^]oa method later to create the database for us At this time, no derived classes have been mapped Later, I will come back to this base entity class to map some derived classes The idea behind entity class inheritance is that the single database table, Od]la, has a database column whose value indicates which entity class the record should be constructed into when it is retrieved by LINQ to SQL That column is known as the discriminator column and is specified using the ?khqij attribute’s Eo@eo_neiej]pkn attribute property A value in the discriminator column is known as the discriminator value or discriminator code When mapping your base entity class to the database table, in addition to the P]^ha attribute, you specify Ejdanep]j_aI]llejc attributes to map discriminator codes to classes derived from the base entity class But at this time, in the preceding Od]la class, no inheritance has been mapped Notice that I have several public members, each being mapped to a database column, and the database column types have been specified Specifying the database column types is necessary in my case, because I will be calling the ?na]pa@]p]^]oa method later, and to so, it must know the appropriate type Also notice that for the Od]la?k`a member, I have specified that the Eo@eo_neiej]pkn attribute property is set to Pnqa, thereby making it the discriminator column This means the Od]la?k`a database column will dictate the entity class type used to construct each record into an entity class object In this class, I have members for the E`, the Od]la?k`a, and the starting X and Y coordinates for the shape on the screen At this time, those are the only members I foresee being common to every shape You may then create a class hierarchy by deriving classes from this base class The derived classes must inherit from the base entity class The derived classes will not specify the P]^ha attribute but will specify ?khqij attributes for each public member that will be mapped to the database Here are my derived entity classes: My Derived Entity Classes Lq^he_?h]ooOmq]na EjdanepoOd]la 8?khqij$@>Pula69Ejp%:[ Lq^he_Se`pd=oEjpacan Aj`?h]oo www.it-ebooks.info 16445ch18.indd 684 7/17/09 2:56:49 PM C H A P T E R N A D D I T I O N A L S Q L C A P A B I LI T I E S 685 Lq^he_?h]ooNa_p]jcha EjdanepoOmq]na 8?khqij$@>Pula69Ejp%:[ Lq^he_Hajcpd=oEjpacan Aj`?h]oo First, for this example, you must forget about the geometric definition for square and rectangle; that is, geometrically speaking, a square is a rectangle, but a rectangle is not necessarily a square In this entity class inheritance example, because a square’s sides must be equal, only one dimension value is needed, width Since a rectangle needs a width and a length, it will inherit from the square and add a member for the length In this sense, from a class inheritance perspective, a rectangle is a square, but a square is not a rectangle While this is backward from the geometric definition, it fits my inheritance entity class model The public members of each of those classes are the members deemed specific to each class For example, since a Omq]na needs a width, it has a Se`pd property Since the Na_p]jcha inherits from the Omq]na, in addition to the inherited Se`pd property, it needs a Hajcpd property I now have my derived classes All I am missing is the mapping between the discriminator values, and the base and derived entity classes Adding the necessary Ejdanep]j_aI]llejc attributes, my base class now looks like this: My Base Entity Class with Derived Class Mappings 8P]^ha([ Ejdanep]j_aI]llejc$?k`a69C(Pula69CapPula$Od]la%(Eo@ab]qhp69Pnqa%([ Ejdanep]j_aI]llejc$?k`a69O(Pula69CapPula$Omq]na%%([ Ejdanep]j_aI]llejc$?k`a69N(Pula69CapPula$Na_p]jcha%%:[ Lq^he_?h]ooOd]la 8?khqij$EoLnei]nuGau69Pnqa(Eo@^Cajan]pa`69Pnqa([ @^Pula69EjpJKPJQHHE@AJPEPU%:[ Lq^he_E`=oEjpacan 8?khqij$Eo@eo_neiej]pkn69Pnqa(@^Pula69JR]n?d]n$.%%:[ Lq^he_Od]la?k`a=oOpnejc 8?khqij$@^Pula69Ejp%:[ Lq^he_Op]npejcT=oEjpacan 8?khqij$@^Pula69Ejp%:[ Lq^he_Op]npejcU=oEjpacan Aj`?h]oo The added mappings map the different discriminator values of the discriminator column to entity classes Since the Od]la?k`a column is the discriminator column, if a record has the value C in that column, that record will get constructed into a Od]la class If a record has an O value in the Od]la?k`a column, that record will get constructed into a Omq]na class And, if a record has an N value in the Od]la?k`a column, that record will get constructed into a Na_p]jcha class www.it-ebooks.info 16445ch18.indd 685 7/17/09 2:56:49 PM 686 CH APT ER 18 N A DDITIONA L S QL C A P A B IL ITIE S Additionally, there must always be a default mapping for when the discriminator column value does not match any discriminator value mapped to an entity class You specify which mapping is the default with the Eo@ab]qhp attribute property In this example, the mapping to the Od]la class is the default So, if a record has the value M in the Od]la?k`a column, that record will get constructed into a Od]la object by default, since it doesn’t match any of the specified discriminator codes That pretty much covers the concept and mappings of entity class inheritance Now, let’s take a look at the entire @]p]?kjpatp: My Entire DataContext Class EilknpoOuopai*@]p]*Hejm*I]llejc L]npe]hLq^he_?h]ooPaop@> Ejdanepo@]p]?kjpatp Lq^he_Od]lao=oP]^ha$KbOd]la% Lq^he_Oq^Jas$>uR]h_kjja_pekj=oOpnejc% Iu>]oa*Jas$_kjja_pekj% Aj`Oq^ Lq^he_Oq^Jas$>uR]h_kjja_pekj=oOuopai*@]p]*E@^?kjja_pekj% Iu>]oa*Jas$_kjja_pekj% Aj`Oq^ Lq^he_Oq^Jas$>uR]h_kjja_pekj=oOpnejc([ >uR]hi]llejcOkqn_a=oOuopai*@]p]*Hejm*I]llejc*I]llejcOkqn_a% Iu>]oa*Jas$_kjja_pekj(i]llejcOkqn_a% Aj`Oq^ Lq^he_Oq^Jas$>uR]h_kjja_pekj=oOuopai*@]p]*E@^?kjja_pekj([ >uR]hi]llejcOkqn_a=oOuopai*@]p]*Hejm*I]llejc*I]llejcOkqn_a% Iu>]oa*Jas$_kjja_pekj(i]llejcOkqn_a% Aj`Oq^ Aj`?h]oo 8P]^ha([ Ejdanep]j_aI]llejc$?k`a69C(Pula69CapPula$Od]la%(Eo@ab]qhp69Pnqa%([ Ejdanep]j_aI]llejc$?k`a69O(Pula69CapPula$Omq]na%%([ Ejdanep]j_aI]llejc$?k`a69N(Pula69CapPula$Na_p]jcha%%:[ Lq^he_?h]ooOd]la 8?khqij$EoLnei]nuGau69Pnqa(Eo@^Cajan]pa`69Pnqa(@^Pula69[ EjpJKPJQHHE@AJPEPU%:[ Lq^he_E`=oEjpacan 8?khqij$Eo@eo_neiej]pkn69Pnqa(@^Pula69JR]n?d]n$.%%:[ Lq^he_Od]la?k`a=oOpnejc www.it-ebooks.info 16445ch18.indd 686 7/17/09 2:56:49 PM C H A P T E R N A D D I T I O N A L S Q L C A P A B I LI T I E S 687 8?khqij$@^Pula69Ejp%:[ Lq^he_Op]npejcT=oEjpacan 8?khqij$@^Pula69Ejp%:[ Lq^he_Op]npejcU=oEjpacan Aj`?h]oo Lq^he_?h]ooOmq]na EjdanepoOd]la 8?khqij$@^Pula69Ejp%:[ Lq^he_Se`pd=oEjpacan Aj`?h]oo Lq^he_?h]ooNa_p]jcha EjdanepoOmq]na 8?khqij$@^Pula69Ejp%:[ Lq^he_Hajcpd=oEjpacan Aj`?h]oo I have done nothing new here other than putting the previously mentioned classes in a WUkqnY@]p]?kjpatp named Paop@> and adding some constructors for it and including the Eilknpo statement for the Ouopai*@]p]*Hejm*I]llejc namespace Now, in Listing 18-3, I will call some code to actually create the database Listing 18-3 Code Creating My Entity Class Inheritance Sample Database @ei`^=oJasPaop@>$[ @]p]Okqn_a9*XOMHATLNAOO7Ejepe]h?]p]hkc9Paop@>7Ejpacn]pa`Oa_qnepu9OOLE7% `^*?na]pa@]p]^]oa$% That code doesn’t have any screen output, but if you check your database server, you should see a database named Paop@> with a single table named Od]la Check the Od]la table to convince yourself that no records exist Now that we have a table, let’s create some data using LINQ to SQL in Listing 18-4 Listing 18-4 Code Creating Some Data for My Entity Class Inheritance Sample Database @ei`^=oJasPaop@>$[ @]p]Okqn_a9*XOMHATLNAOO7Ejepe]h?]p]hkc9Paop@>7Ejpacn]pa`Oa_qnepu9OOLE7% `^*Od]lao*EjoanpKjOq^iep$JasOmq]naSepdw*Se`pd90y% `^*Od]lao*EjoanpKjOq^iep$JasNa_p]jchaSepdw*Se`pd9/(*Hajcpd92y% `^*Od]lao*EjoanpKjOq^iep$JasNa_p]jchaSepdw*Se`pd9 (*Hajcpd91y% `^*Od]lao*EjoanpKjOq^iep$JasOmq]naSepdw*Se`pd92y% `^*Od]lao*EjoanpKjOq^iep$JasNa_p]jchaSepdw*Se`pd90(*Hajcpd93y% `^*Od]lao*EjoanpKjOq^iep$JasOmq]naSepdw*Se`pd95y% `^*Oq^iep?d]jcao$% www.it-ebooks.info 16445ch18.indd 687 7/17/09 2:56:49 PM ... Readers of Pro LINQ: Language Integrated Query in C# 2008 may recall the endless delays in publishing that book A project that initially started as a 9-month project grew into a 17-month project... Welsh Project Manager: Richard Dal Porto Copy Editor: Heather Lang Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositors: Dina Quan, Patrick Cunningham Proofreader:... PART NNN Pro LINQ: Language Integrated Query in VB.NET 2008 CHAPTER Hello LINQ CHAPTER VB.NET 2008 Language Enhancements for LINQ
- Xem thêm -

Xem thêm: 640 pro LINQ , 640 pro LINQ , Query Expression Syntax vs. Standard Dot Notation Syntax, XElement.SetElementValue() on Child XElement Objects

Mục lục

Xem thêm

Gợi ý tài liệu liên quan cho bạn

Nhận lời giải ngay chưa đến 10 phút Đăng bài tập ngay