语言集成查询 (LINQ)有很多值得学习的地方,这里我们主要介绍一下LINQ入门,包括介绍LINQ查询语言等方面。
语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中一项突破性的创新,它在对象领域和数据领域之间架起了一座桥梁。
传统上,针对数据的查询都是以简单的字符串表示,而没有编译时类型检查或 IntelliSense 支持。此外,您还必须针对以下各种数据源学习不同的查询语言:SQL 数据库、XML 文档、各种 Web 服务等。LINQ 使查询成为 C# 和 Visual Basic 中的一等语言构造。应用于所有信息源( all sources of information )的具有多种用途( general-purpose )的语法查询特性( query facilities )。对所有信息源的查询语句,类似数据库的sql语句,xml的xpath 。
LINQ入门代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace HelloVS2008
- {
- public partial class LinqFrm : Form
- {
- public LinqFrm()
- {
- InitializeComponent();
- }
- private void btnLinq_Click(object sender, EventArgs e)
- {
- int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };//信息源
- //var申请的是无类型变量。n没有定义就使用了,类似javascript语法
- //正常的次序:select item from item in items where item>5 orderby item 就是把select item置后
- //与sql的区别:select col1 from table 这里似乎是select col1 from col1 in table 不知何故
- var m = from n in arr where n < 5 orderby n select n;
- foreach (var n in m)
- {
- txtLinq.Text += n;
- }
- }
- }
- }
以上是LINQ入门介绍
【编辑推荐】