MSDN 站点的Linq 101:[联机使用]
http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
很全面的官方站点。
基本概念清楚后,可以参考如下的例子以加深理解
Linq in Action的第5章对于常用模式,性能有个综合的例子,很不错:
以下是一些常见的使用场景举例
IEnumerable<String> titles = SampleData.Books.Select(book => book.Title); String[] array = titles.ToArray(); List<String> list = titles.ToList(); Dictionary<String, Book> isbnRef = SampleData.Books.ToDictionary(book => book.Isbn); Book linqRules = isbnRef["0-111-77777-2"]; |
Dictionary<int, string> frenchNumbers; frenchNumbers = new Dictionary<int, string>(); frenchNumbers.Add(0, "zero"); frenchNumbers.Add(1, "un"); frenchNumbers.Add(2, "deux"); var evenFrenchNumbers = from entry in frenchNumbers where (entry.Key % 2) == 0 select entry.Value; |
List<Book> books = new List<Book>() { new Book { Title="LINQ in Action" }, new Book { Title="LINQ for Fun" }, new Book { Title="Extreme LINQ" } }; var titles = books.Where(book => book.Title.Contains("Action")).Select(book => book.Title); |
var books =SampleData.Books.Select((book, index) => new { index, book.Title }) .OrderBy(book => book.Title); |
var count = "Non-letter characters in this string: 8".Where(c => !Char.IsLetter(c)).Count(); |
Object[] array = { "String", 12, true, 'a' }; var types = array.Select(item => item.GetType().Name) .OrderBy(type => type); |
var query = from book in SampleData.Books where book.Title.Length > 10 orderby book.Price select new { Book=book.Title, book.Price }; |