c# 第六课 linq
LINQ (Language-INtegrated Query)
Programmers perform every day is finding and retrieving (存取) objects in memory, a database, or an XML file.
SQL(relational model) can only search relational database, not object-oriented languages.
LINQ is a bridge over object-oriented languages and relational database.
LINQ is SQL-like, and remove the distinctions(区别) among searching an in-memory data collection, a database, or an XML document. Integrated into C#.
using System; using System.Collections.Generic; using System.Linq; namespace Programming_CSharp { // Simple customer class public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string EmailAddress { get; set; } // Overrides the Object.ToString() to provide a // string representation of the object properties. public override string ToString() { return string.Format("{0} {1}\nEmail: {2}", FirstName, LastName, EmailAddress); } }
这段代码简单的定义了一个队列。
语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中引入的一项创新功能,它在对象领域和数据领域之间架起了一座桥梁。
传统上,针对数据的查询都是以简单的字符串表示,而没有编译时类型检查或 IntelliSense 支持。 此外,您还必须针对以下各种数据源学习一种不同的查询语言:SQL 数据库、XML 文档、各种 Web 服务等等。 LINQ 使查询成为 C# 和 Visual Basic 中的一流语言构造。 您可以使用语言关键字和熟悉的运算符针对强类型化对象集合编写查询。
Deferred(延期的) Query Evaluation(1)
The declaration and initialization of a query expression do not actually execute the query
IEnumerable<Customer> result = from customer in customers where customer.FirstName == "Donna“ select customer;
Instead, a LINQ query is executed, or evaluated, when you iterate through the query result:
static void Main() { List<Customer> customers = CreateCustomerList(); List<Address> addresses = CreateAddressList(); // Find all addresses of a customer var result = from customer in customers join address in addresses on string.Format("{0} {1}", customer.FirstName,customer.LastName) equals address.Name orderby customer.LastName, address.Street descending select new { Customer = customer, Address = address }; foreach (var ca in result) { Console.WriteLine(string.Format("{0}\nAddress: {1}", ca.Customer, ca.Address)); } }
var关键字 可以赋予局部变量推断“类型”var 而不是显式类型。var 关键字指示编译器根据初始化语句右侧的表达式推断变量的类型。推断类型可以是内置类型、匿名类型、用户定义类型、.NET Framework 类库中定义的类型或任何表达式。
上面的资料有点抽象不好理解.
示例:
原来我们定义变量,是要这样:
数据类型 变量名 = 值;
如:
int a = 1;
string b = "2";
也就是说,"必须先明确地"指定你的变量是什么数据类型,才能给它赋值.这点很重要,要记住才好比较.
现在在C# 3.0里,有了变化,就是可以不用像上面那样定义变量了.
如:
var a =1 ;
那这个a,是什么类型呢?和原来的做法不一样了.
奥妙就在这里,IDE或编译器会根据你给a 的值:1,来"推论,断定"a是一个整数类型.
同理:
var b = "2";
因为给b的值是"2"这样一个字符串,所以,b就是string类型...
Grouping and the group Keyword
An other powerful feature of LINQ, commonly used by SQL programmers but now integrated into the language itself, is grouping。
var result = from address in addresses group address by address.Name; foreach (var gp in result) { Console.WriteLine("{0}", gp.Key); foreach (var a in gp) Console.WriteLine("\t{0}", a); }
运用group关键字。group 子句返回一个 IGrouping<TKey,TElement>对象序列,这些对象包含零个或更多个与该组的键值匹配的项.
可以用 group 子句结束查询表达式,如下面的示例所示:
// Query variable is an IEnumerable<IGrouping<char, Student>> var studentQuery1 = from student in students group student by student.Last[0];