Linq 简明教程

LINQ - Environment Setup

在开始使用 LINQ 程序前,最好先了解 LINQ 环境的设置细节。LINQ 需要一个 .NET 框架,这是一种革命性的平台,可拥有各种应用程序。LINQ 查询既可以用 C# 也可用 Visual Basic 方便地编写。

Before starting with LINQ programs, it is best to first understand the nuances of setting up a LINQ environment. LINQ needs a .NET framework, a revolutionary platform to have a diverse kind of applications. A LINQ query can be written either in C# or Visual Basic conveniently.

微软通过 Visual Studio 为这两种语言 (即 C# 和 Visual Basic) 提供工具。我们的示例全部在 Visual Studio 2010 中编译和编写。但是,Visual Basic 2013 版也可以使用。它是最新版本,并且与 Visual Studio 2012 有许多相似之处。

Microsoft offers tools for both of these languages i.e. C# and Visual Basic by means of Visual Studio. Our examples are all compiled and written in Visual Studio 2010. However, Visual Basic 2013 edition is also available for use. It is the latest version and has many similarities with Visual Studio 2012.

Getting Visual Studio 2010 Installed on Windows 7

Visual Studio 可以从安装介质(如 DVD)中安装。成功在系统中安装 Visual Basic 2010 需要管理员凭据。在安装之前,必须断开系统中所有可移动 USB,否则安装可能会失败。以下列出了一些安装中必备的硬件要求。

Visual Studio can be installed either from an installation media like a DVD. Administrator credentials are required to install Visual Basic 2010 on your system successfully. It is vital to disconnect all removable USB from the system prior to installation otherwise the installation may get failed. Some of the hardware requirements essential to have for installation are the following ones.

Hardware Requirements

  1. 1.6 GHz or more

  2. 1 GB RAM

  3. 3 GB(Available hard-disk space)

  4. 5400 RPM hard-disk drive

  5. DirectX 9 compatible video card

  6. DVD-ROM drive

Installation Steps

Step 1 − 首先在插入 Visual Studio 2010 组件包 DVD 后,单击屏幕上弹出框中出现的 Install or run program from your media

Step 1 − First after inserting the DVD with Visual Studio 2010 Package, click on Install or run program from your media appearing in a pop-up box on the screen.

Step 2 − 现在屏幕上会出现 Visual Studio 的设置。选择 Install Microsoft Visual Studio 2010

Step 2 − Now set up for Visual Studio will appear on the screen. Choose Install Microsoft Visual Studio 2010.

linq environment 1

Step 3 − 单击后,进程将启动,并且屏幕上会出现设置窗口。在完成安装组件的加载(这需要一些时间)后,单击 Next 按钮以移至下一步。

Step 3 − As soon as you will click, it the process will get initiated and a set up window will appear on your screen. After completion of loading of the installation components which will take some time, click on Next button to move to the next step.

linq environment 2

Step 4 − 这是安装的最后一步,并且将出现一个开始页面,在其中只需选择“我已阅读并接受许可证条款”,然后单击 Next 按钮。

Step 4 − This is the last step of installation and a start page will appear in which simply choose "I have read and accept the license terms" and click on Next button.

linq environment 3

Step 5 − 现在从屏幕上出现的选项页面中选择要安装的功能。您可以选择 FullCustom 选项。如果您拥有的磁盘空间少于磁盘空间要求中显示所需的磁盘空间,请使用“自定义”。

Step 5 − Now select features to install from the options page appearing on your screen. You can either choose Full or Custom option. If you have less disk space than required shown in the disk space requirements, then go for Custom.

linq environment 4

Step 6 − 选择“自定义”选项,以下窗口将出现。选择要安装的功能,然后单击 Update ,否则请转到步骤 7。但是,建议不要使用自定义选项,因为在将来,您可能需要您选择不安装的功能。

Step 6 − When you choose Custom option, the following window will appear. Select the features that you want to install and click Update or else go to step 7. However, it is recommended not to go with the custom option as in future, you may need the features you have chosen to not have.

linq environment 5

Update − 不久后将显示一个弹出窗口,并且安装将开始,这可能需要很长时间。请记住,这是为了安装所有组件。

Step 7 − Soon a pop up window will be shown and the installation will start which may take a long time. Remember, this is for installing all the components.

linq environment 6

Step 8 − 最后,您将可以在窗口中看到一条消息,提示安装已成功完成。单击 Finish

Step 8 − Finally, you will be able to view a message in a window that the installation has been completed successfully. Click Finish.

linq environment 7

Writing C

  1. Start Visual Studio 2010 Ultimate edition and choose File followed by New Project from the menu.

  2. A new project dialog box will appear on your screen.

  3. Now choose Visual C# as a category under installed templates and next choose Console Application template as shown in figure below.

linq Csharp program 1
  1. Give a name to your project in the bottom name box and press OK.

  2. The new project will appear in the Solution Explorer in the right-hand side of a new dialog box on your screen.

linq Csharp program 2
  1. Now choose Program.cs from the Solution Explorer and you can view the code in the editor window which starts with ‘using System’.

  2. Here you can start to code your following C# program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld {
   class Program {
      static void Main(string[] args) {

         Console.WriteLine("Hello World")
         Console.ReadKey();
      }
   }
}
  1. Press F5 key and run your project. It is highly recommended to save the project by choosing FileSave All before running the project.

Writing VB Program using LINQ in Visual Studio 2010

  1. Start Visual Studio 2010 Ultimate edition and choose File followed by New Project from the menu.

  2. A new project dialog box will appear on your screen.

  3. Now chose Visual Basic as a category under installed templates and next choose Console Application template.

  4. Give a name to your project in the bottom name box and press OK.

  5. You will get a screen with Module1.vb. Start writing your VB code here using LINQ.

Module Module1

   Sub Main()
      Console.WriteLine("Hello World")
      Console.ReadLine()
   End Sub

End Module
  1. Press F5 key and run your project. It is highly recommended to save the project by choosing FileSave All before running the project.

当上面 C# 或 VB 代码 cimpiled 并运行时,它会产生以下结果 -

When the above code of C# or VB is cimpiled and run, it produces the following result −

Hello World