如何使用LINQ和lambda语法创建匿名类型?

时间:2010-09-05 08:39:03

标签: c# linq

我有一个使用lambda语法的LINQ查询:

var query =
    books
        .Where(book => book.Length > 10)
        .OrderBy(book => book.Length)

我想创建一个匿名类型来存储投影,类似于:

var query = from book in books
            where book.Length > 10
            orderby book
            select new { Book = book.ToUpper() };

如何在lambda语法中“选择新”?

谢谢,

斯科特

1 个答案:

答案 0 :(得分:42)

像这样:

var query =
    books
        .Where(book => book.Length > 10)
        .OrderBy(book => book.Length)
        .Select(book => new { Book = book.ToUpper() });