您好,欢迎访问三七文档
张义先开发工程师开发程师中达金桥技术服务有限公司微软微软中文技术论坛中文技术论坛————精彩生活每一天精彩生活每一天://本周活动更新本周活动更新:9Top10论坛英雄!9畅谈我的2007畅谈我的与众不同:9版主:50+微软最有价值专家(MVP)9涵盖微软几乎所有产品线和知识库930+适合开发人员和IT专业人员技术板块适合开发人员和专人员技术板块数据访问方法比较LINQ架构介绍LINQToObject介绍LINQToObject介绍DemoToday’sProblem:Data!=ObjectsData!=ObjectsDataAccessInAPIsTodayDataAccessInAPIsTodayDataAccessInAPIsTodayDataAccessInAPIsTodaySqlConnectionc=newSqlConnection(…);c.Open();SqlCommandcmdnewSqlCommand(QueriesinquotesSqlCommandcmd=newSqlCommand(@SELECTc.Name,c.PhoneFROMCustomerscWHEREcCity=@p0ArgumentslooselyboundWHEREc.City=@p0);cmd.Parameters.AddWithValue(@po,London);DataReaderdr=c.Execute(cmd);looselyboundDataReaderdrc.Execute(cmd);while(dr.Read()){stringname=dr.GetString(0);stringphone=dr.GetString(1);Resultslooselytypedgpg();DateTimedate=dr.GetDateTime(2);}dr.Close();CompilercannothelpcatchmistakesDataAccesswithLINQDataAccesswithLINQDataAccesswithLINQDataAccesswithLINQClassesdescribepublicclassCustomer{publicintCustomerId;dataEncapsulatedpublicstringName;publicstringCity;bliidlid(){BusinessValidationpublicvoidValidate(){//Todo:AddBusinessValidation}}Queryisnaturalpartofthelanguage}GridView1.DataSource=fromcustomerindb.CustomerswherecustomerCity==Londonwherecustomer.City==Londonselectcustomer;GridView1DataBind();ThecompilerprovidesGridView1.DataBind();providesintellisenseandtype-checkingLINQ架构LINQ架构LINQ架构LINQ架构VBVBOthers…Others…C#C#.NetLanguageIntegratedQuery(LINQ).NetLanguageIntegratedQuery(LINQ)LINQenableddatasourcesLINQenableddatasourcesLINQenabledADONETLINQenabledADONETLINQLINQLINQLINQLINQenabledADO.NETLINQenabledADO.NETLINQLINQLINQLINQLINQLINQLINQLINQToObjectsToObjectsLINQLINQToXMLToXMLLINQLINQToEntitiesToEntitiesLINQLINQToSQLToSQLLINQLINQToDatasetsToDatasetsbooktitle/author/ObjectsObjectsprice//bookXMLXMLRelationalRelationalSearching/SortinganArraySearching/SortinganArraySearching/SortinganArraySearching/SortinganArrayArrayimplementsstring[]cities={“Auckland”,“Oslo”,“Sydney”,ypIEnumerableT“Seattle”,“Paris”,“LosAngeles”};IEnumerablestringplaces=fromcityincitieswherecity.Length5orderbycitydescendingselectcity;GridView1.DataSource=places;GridView1.DataBind();LINQQueryyExpressionusingQueryOperatorsIEnumerablestringIEnumerablestringsequenceresultcanbeusedw/databindingSearching/SortinganArraySearching/SortinganArraySearching/SortinganArraySearching/SortinganArrayCustomCityClassCustomCityClassCustomCityClassCustomCityClasspublicclassCity{publicstringName;publicstringCountry;publicintDistanceFromSeattle;}}ListCitylocations=GetLocations();Searching/SortingCityCollectionSearching/SortingCityCollectionSeacg/SotgCtyCoectoSeacg/SotgCtyCoectoCollectionimplementsListCitylocationsGetLocations();pIEnumerableTListCitylocations=GetLocations();IEnumerableCityplaces=fromcityinlocationswherecity.DistanceFromSeattle1000wherecity.DistanceFromSeattle1000orderbycity.Country,city.Nameselectcity;GridView1.DataSource=places;GridView1.DataBind();IEnumerableCityreturnsequencedeterminedbyyselectstatementGridViewDeclarationGridViewDeclarationGridViewDeclarationGridViewDeclarationasp:GridViewID=GridView1AutoGenerateColumns=falserunat=serveraspGdeGdeutoGeeateCousaseuatseeColumnsasp:BoundFieldHeaderText=CountryDataField=Country/asp:BoundFieldHeaderText=CityDataField=Name/BdFildHdTt“Dit”DtFildDitFSttl“/asp:BoundFieldHeaderText=“Dist”DataField=DistanceFromSeattle“//Columns/asp:GridViewSearching/SortingCityCollectionSearching/SortingCityCollectionSearching/SortingCityCollectionSearching/SortingCityCollectionLINQ需求和特性LINQ需求和特性LINQ需求和特性LINQ需求和特性有您想修转查询数有您想修转查询数•有时您想修改或者转化查询返回的数据•有时您想修改或者转化查询返回的数据•利用编译器对”匿名类型”的支持可以放心的•利用编译器对”匿名类型”的支持可以放心的使用LINQ使用LINQ匿名类型匿名类型匿名类型匿名类型ListCitycities=CityUtilityHelper.GetCities();varplaces=fromcityincitieshiilwherecity.DistanceFromSeattle1000selectnew{City=city.Name,CtitCtCountry=city.Country,DistanceInKm=city.DistanceFromSeattle*1.61};GridView1.DataSource=places;GridView1.DataBind();Anonymoustypeusedtocustomshapedataresultsdlilkiltandapplymiles-kilometerconversion匿名类型匿名类型匿名类型匿名类型asp:GridViewID=GridView1AutoGenerateColumns=falserunat=serverColumnsdilddild/asp:BoundFieldHeaderText=CountryDataField=Country/asp:BoundFieldHeaderText=CityDataField=“City/asp:BoundFieldHeaderText=Dist(KM)DataField=DistanceInKm//Columns/Columns/asp:GridView匿名类型匿名类型匿名类型匿名类型LINQtoObjects总结LINQtoObjects总结LINQtoObjects总结LINQtoObjects总结使用C#和VB进行开发的灵活的查询语法使用C#和VB进行开发的灵活的查询语法•使用C#和VB进行开发的灵活的查询语法跟类似的查询句法•使用C#和VB进行开发的灵活的查询语法跟类似的查询句法•跟SQL类似的查询句法•跟SQL类似的查询句法•功能强大的查询的语言•功能强大的查询的语言•LINQ的查询结果序列可以被绑定到任何ASPNET中的数据绑定控件•LINQ的查询结果序列可以被绑定到任何ASPNET中的数据绑定控件ASP.NET中的数据绑定控件ASP.NET中的数据绑定控件SummarySummarySummarySummary•LINQ将会成为一门非常重要的技术•LINQ将会成为一门非常重要的技术•LINQ将会成为一门非常重要的技术集成的语言特性可以改进和提高生产力和进行•LINQ将会成为一门非常重要的技术集成的语言特性可以改进和提高生产力和进行•集成的语言特性可以改进和提高生产力和进行错误检测•集成的语言特性可以改进和提高生产力和进行错误检测•使用LINQw/ASP.NET都是非常简单和有趣的•使用LINQw/ASP.NET都是非常简单和有趣的事情事情Question&AnswerQuestion&Ans
本文标题:Visual Studio 2008开发新特性系列课程(3):VS2008新特性之LINQ入门
链接地址:https://www.777doc.com/doc-3354777 .html