Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can anonymous types be created using LINQ with lambda syntax?

Tags:

c#

linq

I have a LINQ query that uses lambda syntax:

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

I would like to create an anonymous type to store the projection, similar to:

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

How do I "select new" in lambda syntax ?

Thanks,

Scott

like image 650
Scott Davies Avatar asked Sep 05 '10 08:09

Scott Davies


1 Answers

Like this:

var query =
    books
        .Where(book => book.Length > 10)
        .OrderBy(book => book.Length)
        .Select(book => new { Book = book.ToUpper() });
like image 172
Fredrik Mörk Avatar answered Oct 19 '22 13:10

Fredrik Mörk