>>51aspx首页 | >>Winform源码 | >>.Net源码大搜捕 | >>问题/帮助

Asp.net源码交流论坛

登录 注册
  • 标签
  • 会员
  • 搜索
  • 帮助

Asp.net源码交流论坛 » Asp.net交流讨论区 » Asp.net技术问答 » 将VS2005的项目转换到VS2008—使 LINQ能够使用

帖子标题
业务申请区
  • 实名认证申请
  • 免费.Net主机申请
  • 积分奖励申请
  • 有偿服务申请
Asp.net交流讨论区
  • Asp.net技术问答
  • .Net项目、服务交易区
  • .Net源码问答区
  • Asp.net技术文章
  • 常用工具下载
Asp.net资源发布区
  • [源码发布区]
  • [视频发布区]
  • [商业代码区]
  • [其他.net相关资源]
Asp.net专题讨论区
  • Ajax/Atlas无刷新技术
  • 面向对象开发
  • ADO.net讨论区
  • 控件专题讨论
源码环境搭建和部署视频! 源码使用常见问题 51Aspx自有服务产品 开业特惠 快速通道:[源码发布] | [实名认证]
1/1页1 跳转到页查看:4025
发新话题 回复该主题
键盘左右键可以进行前后翻页操作
帮助

将VS2005的项目转换到VS2008—使 LINQ能够使用

离线 iloveaspx
头像

  • [版主]
  • [122]
  • 104
  • 2007-05-26
iloveaspx 2008-02-15 12:46 | 只看楼主 树型| 收藏| 小 中 大 1 #

将VS2005的项目转换到VS2008—使 LINQ能够使用



原文作者:Benth Massi
原文链接:Converting VS 2005 Projects to VS 2008 - Enabling LINQ
          假如现在你有一个用Visual Studio 2005 开发的应用程序你想进一步挖掘和转换它,并开始使用VS2008中的 LINQ功能。那么在这里,我打算简单介绍下使LINQ正常工作, 你需要什么步骤, 这些步骤也同样取决于你需要用到LINQ的什么功能。
          在Visual Studio 2008 中,有多种的对象特性使你能够用.NET 2.0, 3.0 或者3.5框架来在2008的环境中编写程序。Scott Gu的文章 和Rick Strahl的对此都有介绍。这就意味着你不需要为了正常运行先前版本的程序而去安装多个版本的Visual Studio IDE在你的电脑上(注意,但是如果你先前用.NET 1.0 或 1.1开发的,那么还是需要安装2002或2003IDE的)。这是个好消息, 因为它不但节省了磁盘空间和省去了前后的转换,还能够使你获得更多的有利条件像调试和编辑而不需要冒着升级项目文件的风险。但是如果你希望把项目升级成3.5的版本去使用LINQ功能,你需要自己添加一些新的命名空间。
        当你第一次在Visual Studio 2008 中打开一个Visual Studio 2005的项目时,系统会提示升级你的项目。实际上这样做是为了升级了你的项目文件(.vbproj) 和解决方案 (.sln) 使其和2008兼容。而这个项目仍然可以用2005打开,它是向下兼容的。但是解决方案文件是单一的,因此如果你是个团队开发项目并且混合有2005和2008 两种版本IDE时,你需要保留两个解决方案,不过幸运的是,你的项目文件(比解决方案要有更多的变化)是可以共享的。
        如今所有的这些升级过程所做的是同时升级项目文件和解决方案,你的程序仍然以.NET2.0的平台为对象,为了升级你的程序去使用新的特性比如LINQ功能,你需要去改变到需要的Framework版本和添加一些新的引用。你也希望根据那些特性打开新的推断特性的选项,这允许编译器通过估计右值表达式去推测出局部变量的申明类型。这将对编写LINQ的查询非常有用。为了支持它,在解决方案资源管理器中双击我的项目打开项目属性,选择编译标签,选择下方的“On”按钮。
        现在去改变目标Framework,点击"Advanced Compile Options..."

        选择Framework 3.5,按ok,项目就会被关闭后重新开启。如果你再次打开项目看引用标签你会发现System.Core.dll 3.5的版本被自动的引用了。但是为了能使用LINQ,还需要引入两个命名空间和一些和LINQ有关的引用,为了完全支持Linq to Objects,需要添加 System.Linq的引用。现在你可以编写查询对象的代码如下:

[复制到剪贴板]
CODE:

Dim currentFiles = From File In My.Computer.FileSystem.GetFiles(CurDir) _
                  Select My.Computer.FileSystem.GetFileInfo(File)


为了能编写出作用于DataSet数据集的查询,你需要添加System.Data.DataSetExtensions的引用, 之后你要重新运行与你想编写的LINQ程序相关的DataSet的生成器。右键DataSet 选“Run Custom Tool”,  如此会重新产生DataSet 编码,这个编码可以使DataTables  继承位于System.Data.DataSetExtension 命名空间里 的一个叫做TypedTableBase的LINQ-ready 的类。  接下来你就可以在已经被定义的DataSet上编写程序了,例如:

[复制到剪贴板]
CODE:

Dim total = Aggregate Products In Me.CategoryProductDataSet.Products _
            Where Products.CategoryID = 1 AndAlso _
                  Products.Discontinued = False _
            Into Sum(Products.UnitPrice * Products.UnitsInStock) 


如果你要开始使用LINQ 对XML 支持,你需要添加向System.Xml.Linq.dll的引用以及加入 System.Xml.Linq 命名空间,之后你就可以编写作用于xml的语句了 如:

[复制到剪贴板]
CODE:

Dim survey = XElement.Load(CurDir() & "\questions.xml")

Dim questions = From q In survey...<question> Select q


最后,如果你想要在你最新升级的项目中使用LINQ to SQL,那也是相当简单的。只要右键选择添加新的项目并选择“LINQ to SQL”类模板,它将会开启一个新的O/R设计器并且自动的为你添加System.Data.Linq.dll的引用。这允许你编写类似下面的查询(依靠SQL-server):

[复制到剪贴板]
CODE:

Dim countryList = From Customer In Db.Customers _
                  Where Customer.Country <> &amp;amp;amp;quot;&amp;amp;amp;quot; _
                  Order By Customer.Country _
                  Select Customer.Country Distinct


希望我已经清楚的呈现了怎么把你当前的项目中转换到VS2008使用LINQ的第一步, 那么你还等什么呢。
最后编辑iloveaspx 最后编辑于 2008-02-15 12:59:50
广告位招租,我换新头像了阿
 

TOP

 

发送短消息

查看公共资料

查找该会员全部帖子

  • 26
  • 2
  • 8 分
  • 19.8 元
  • 离线
离线 iloveaspx
头像

  • [版主]
  • [122]
  • 104
  • 2007-05-26
iloveaspx 2008-02-15 12:51 | 只看楼主 树型| 收藏| 小 中 大 2 #

回复:将VS2005的项目转换到VS2008—使 LINQ能够使用



因为原文:
Converting VS 2005 Projects to VS 2008 - Enabling LINQ
Published 21 August 07 02:36 PM
So you have an application you've written in Visual Studio 2005 and you want to dig in, convert this baby, and start using LINQ in Visual Studio 2008. In this post I'm going to outline what steps are involved to get LINQ working based on what providers you may want to use.

In Visual Studio 2008 there's a new multi-targeting feature that allows you to work in Visual Studio 2008 but be able to write applications targeting either the .NET 2.0, 3.0 or 3.5 Frameworks. ScottGu has a good post that talks about this and so does Rick Strahl. This means that going forward you won't have to load multiple versions of the Visual Studio IDE onto your machine in order to work on applications targeting previous versions of the framework. (Note: If you are targeting .NET 1.0 or 1.1 frameworks then you'll still have to load 2002 or 2003 IDEs.) This is good news because it not only saves disk space and context switching, it allows you to take advantage of the new IDE tools like debuggers and editors without the risk of upgrading your current projects. However when you do want to upgrade a project to 3.5 to start taking advantage of LINQ, you need to import some of the new namespaces yourself.

When you first open up a project written in Visual Studio 2005 inside Visual Studio 2008 it will prompt you to upgrade the project. What this actually does is update the project (.vbproj) and solution .(sln) files to be compatible with VS 2008. The project file is actually still backwards compatible with VS 2005 so you can still open it there, the solution file however, is just one way. So if you have a team of developers working with a mix of both VS 2005 and VS 2008 IDEs you'll need to keep two solution files, but luckily your project files (which change much more often than solution configurations) can be shared.

Now all this upgrade process does is upgrade your project and solution files, your application will still be targeting the .NET 2.0 Framework. In order to upgrade your apps to use new features like LINQ you'll need to change the target framework and add some references. You'll also want to turn on the new Option Infer feature. This allows the compiler to infer local variable type declarations by evaluating the right-hand side of the expression. This becomes extremely useful when writing LINQ queries. To enable this, double-click on My Project in the Solution Explorer to open the project properties and select the Compile tab. Under Option Infer select "On".

Now to change the target framework, click the "Advanced Compile Options..." button and you can change the target in the dropdown:



Select the 3.5 Framework, click OK, and the project will be closed and reopened. If you open the project properties again and look on the References tab you'll notice that the System.Core.dll version 3.5 is now being referenced automatically for you. However, in order to start using LINQ you'll need to import a couple namespaces and add a couple more references depending on which LINQ providers you want to use. To enable pure LINQ to Objects, on the References tab under Imported Namespaces select System.Linq. You'll now be able to write queries that work over objects like:

Dim currentFiles = From File In My.Computer.FileSystem.GetFiles(CurDir) _

                  Select My.Computer.FileSystem.GetFileInfo(File)

In order to write queries that work over DataSets you'll need to add a reference to System.Data.DataSetExtensions then you'll need to re-run the Dataset generator on the DataSets you want to write LINQ queries over. Just right-click on the DataSet and select "Run Custom Tool". This will regenerate the DataSet code so that the DataTables now inherit from a LINQ-ready class called TypedTableBase which is in the System.Data.DataSetExtension namespace. You'll now be able to write queries that work over typed DataSets. For instance, you can now write:

Dim total = Aggregate Products In Me.CategoryProductDataSet.Products _

            Where Products.CategoryID = 1 AndAlso _

                  Products.Discontinued = False _

            Into Sum(Products.UnitPrice * Products.UnitsInStock)               


If you want to start using the LINQ to XML provider you'll need to add a reference to System.Xml.Linq.dll and import the namespace System.Xml.Linq. You'll now be able to write queries that work over XML like:

Dim survey = XElement.Load(CurDir() & "\questions.xml")


Dim questions = From q In survey...<question> Select q

Finally, if you want to start using LINQ to SQL classes in your newly upgraded project, it's really easy. Just right-click and select Add New Item and select the LINQ to SQL classes template which opens up the new O/R designer and automatically adds the reference to System.Data.Linq.dll for you. This allows you to write queries like this (against SQL-server):

Dim countryList = From Customer In Db.Customers _

                  Where Customer.Country <> "" _

                  Order By Customer.Country _

                  Select Customer.Country Distinct


So I hope that clears up how to get started with LINQ in your current applications that you're bringing into VS 2008. So what are you waiting for? ;-) Happy LINQing.

Enjoy!
广告位招租,我换新头像了阿
 

TOP

 

发送短消息

查看公共资料

查找该会员全部帖子

  • 26
  • 2
  • 8 分
  • 19.8 元
  • 离线
<<上一主题|下一主题>>
1/1页1 跳转到页
发表新主题 回复该主题

相关主题

大侠相助
LINQ TO OBJECTS
silverlight小制作(手机功能演示)
没法运行,有错误
1分钟实现无闪刷新!.NET2008集成Ajax
  • 发新主题

Asp.net源码下载专业站  - 源码推荐 - 最新源码  Sitemap

bbs.51Aspx.com - 简洁版本 - TOP - 界面风格

  • Default

Discuz!NT

Powered by Discuz!NT 2.6.1 © 2000-2010 51Aspx.com.

Processed in 0.125 second(s) , 5 queries. 京ICP备06046876号

  • 我的资料
  • 我的主题
  • 我的回复
  • 我的精华
  • 我的附件
  • 我的收藏
  • 基本状况
  • 流量统计
  • 客户软件
  • 发帖量记录
  • 版块排行
  • 主题排行
  • 发帖排行
  • 积分排行
  • 在线时间
帖子标题
作  者