result = from book in books where book.Title.StartsWith("I") orderby book.Price select new { Key=book.Title,Value = book.Price};
从books集合里枚举一个book,如果这个book的标题是以”I”开头的就把它加入到返回集合中,并把返回集合按照book的价钱排序将上面的代码编译后的程序集用Reflector反编译,生成的代码是:books.Where<Book>(delegate(Book book)
{
return book.Title.StartsWith("I");
}).OrderBy<Book, float>(delegate(Book book)
{
return book.Price;
}).Select(delegate(Book book)
{
return new { Key = book.Title, Value = book.Price };
});
{
return book.Title.StartsWith("I");
}).OrderBy<Book, float>(delegate(Book book)
{
return book.Price;
}).Select(delegate(Book book)
{
return new { Key = book.Title, Value = book.Price };
});
==========================
//这样的一个集合,存储在内存中
IList<Book> books = new List<Book> {
new Book { Title = "Inside COM", ISBN = "123-456-789",Price=20 },
new Book { Title = "Inside C#", ISBN = "123-356-d89",Price=100 },
new Book { Title = "Linq", ISBN = "123-d56-d89", Price = 120 }
};
数据查询阶段IList<Book> books = new List<Book> {
new Book { Title = "Inside COM", ISBN = "123-456-789",Price=20 },
new Book { Title = "Inside C#", ISBN = "123-356-d89",Price=100 },
new Book { Title = "Linq", ISBN = "123-d56-d89", Price = 120 }
};
var result = from book in books
where book.Price > 50
orderby book.Price
select new { Key = book.Title, Value = book.Price };
foreach (var item in result)
Console.WriteLine("Key:{0}-Value:{1}",item.Key,item.Value.ToString());
where book.Price > 50
orderby book.Price
select new { Key = book.Title, Value = book.Price };
foreach (var item in result)
Console.WriteLine("Key:{0}-Value:{1}",item.Key,item.Value.ToString());