标签:des style blog http color 使用 os io
语言集成查询 (LINQ) 是 Visual Studio 2008 中引入的一组功能,可为 C# 和 Visual Basic 语言语法提供强大的查询功能。 LINQ 引入了标准易学的数据查询和更新模式,可以扩展该方法来支持任何类型的数据存储。 Visual Studio 包括 LINQ 提供程序集,后者支持将 LINQ 与 .NET Framework 集合、SQL Server 数据库、ADO.NET 数据集和 XML 文档结合使用。
LINQ特有的编程结构:
图是从Linq In Action 中截的。
下面介绍下Linq To Object:(查询内存中的集合)
这是我们查询所用的数据源,(Linq To Action中的)
using System; using System.Collections.Generic; using System.Text; namespace LinqInAction.LinqBooks.Common { static public class SampleData { static public Publisher[] Publishers = { new Publisher {Name="FunBooks"}, new Publisher {Name="Joe Publishing"}, new Publisher {Name="I Publisher"} }; static public Author[] Authors = { new Author {FirstName="Johnny", LastName="Good"}, new Author {FirstName="Graziella", LastName="Simplegame"}, new Author {FirstName="Octavio", LastName="Prince"}, new Author {FirstName="Jeremy", LastName="Legrand"} }; static public Subject[] Subjects = { new Subject {Name="Software development"}, new Subject {Name="Novel"}, new Subject {Name="Science fiction"} }; static public Book[] Books = { new Book { Title="Funny Stories", Publisher=Publishers[0], Authors=new[]{Authors[0], Authors[1]}, PageCount=101, Price=25.55M, PublicationDate=new DateTime(2004, 11, 10), Isbn="0-000-77777-2", Subject=Subjects[0] }, new Book { Title="LINQ rules", Publisher=Publishers[1], Authors=new[]{Authors[2]}, PageCount=300, Price=12M, PublicationDate=new DateTime(2007, 9, 2), Isbn="0-111-77777-2", Subject=Subjects[0] }, new Book { Title="C# on Rails", Publisher=Publishers[1], Authors=new[]{Authors[2]}, PageCount=256, Price=35.5M, PublicationDate=new DateTime(2007, 4, 1), Isbn="0-222-77777-2", Subject=Subjects[0] }, new Book { Title="All your base are belong to us", Publisher=Publishers[1], Authors=new[]{Authors[3]}, PageCount=1205, Price=35.5M, PublicationDate=new DateTime(2006, 5, 5), Isbn="0-333-77777-2", Subject=Subjects[2] }, new Book { Title="Bonjour mon Amour", Publisher=Publishers[0], Authors=new[]{Authors[1], Authors[0]}, PageCount=50, Price=29M, PublicationDate=new DateTime(1973, 2, 18), Isbn="2-444-77777-2", Subject=Subjects[1] } }; } }
一、概述
LINQ to OBJECT是用于操作内存对象的LINQ编程接口,包含了大量的查询操作符,针对内存中的集合对象进行操作。LINQ TO OBJECT的大部分操作是针对序列的。标准查询操作符本质是一些扩展方法。
二、延时标准查询操作符
I、延时标准查询操作符是指具备延时查询特性的标准查询操作符,这些操作符构成了LINQ TO OBJECT编程接口的最主要内容。
主要包括:(以下示例中,一般第一个方法为Linq扩展方法,第二个方法为查询表达式语法)
var books = SampleData.Books.Where(b => b.Price > 20); var books1 = from book in SampleData.Books where book.Price > 30 select book; //多条件直接用“&&”或者“||”来组合 var book2 = SampleData.Books.Where(b => b.Price > 20 && b.PageCount > 500);
var books = SampleData.Books.GroupBy(book => book.Publisher).Select(publisherBooks => new { Publisher = publisherBooks.Key.Name, books = publisherBooks }); var books = from publisher in SampleData.Publishers from book in SampleData.Books select new { Correct = publisher == book.Publisher, Publisher = publisher.Name, Book = book.Title };
var authors = SampleData.Books.SelectMany(book => book.Authors).Distinct().Select(author => author.FirstName + " " + author.LastName); var authors2 = SampleData.Books.SelectMany(book => book.Authors) .Select(author => author.FirstName + " " + author.LastName);
var books = from publisher in SampleData.Publishers join book in SampleData.Books on publisher equals book.Publisher select new { Publisher = publisher.Name, Books = book.Title }; var books2 = SampleData.Publishers.Join(SampleData.Books, publisher => publisher, book => book.Publisher, (publisher, book) => new { Publisher = publisher.Name, Book = book.Title }); //Join 实现 Left Join var books = from publisher in SampleData.Publishers join book in SampleData.Books on publisher equals book.Publisher into publisherBooks from book in publisherBooks.DefaultIfEmpty() select new { Publisher = publisher.Name, Book = book == default(Book) ? "no books" : book.Title };
var books = SampleData.Books.GroupBy(book => book.Publisher).Select(publisherBooks => new { Publisher = publisherBooks.Key.Name, books = publisherBooks }); var books = from book in SampleData.Books group book by book.Publisher into publisherBooks select new { Publisher = publisherBooks.Key.Name, books = publisherBooks, Count = publisherBooks.Count() };
ArrayList booksList = new ArrayList(SampleData.Books); var query = from book in booksList.Cast<Book>() where book.PageCount > 150 select new { book.Title, book.PageCount }; var query2 = from Book book in booksList where book.PageCount > 150 select new { book.Title, book.PageCount };
II、非延时标准查询操作符
非延时标准查询操作符是指不具备昝查询特性的标准查询操作符,这些操作符一般用于辅助延时标准查询操作符使用。
主要包括:
static void Main(string[] args) { var numbers = GetArray(5); //阶乘示例 var result = (from n in numbers select n).Aggregate( (total, next) => { return total * next; }); Console.WriteLine("5的阶乘为:{0}",result);//返回120,也就是1*2*3*4*5 } static IEnumerable<int> GetArray(int max) { List<int> result = new List<int>(max); for (int i = 0; i < max; i++) { result.Add(i+1); } return result; }
由于精力有限,例子不够丰富,不过基本的语义解释清楚了,语法应该很好懂,有时间再把示例语句补上,本人不怎么会排版,排版很烂,会慢慢改进,欢迎指教任何问题,谢谢。
Linq to Object 的简单使用示例,布布扣,bubuko.com
标签:des style blog http color 使用 os io
原文地址:http://www.cnblogs.com/moretry/p/3905877.html