Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views1 page

Projection/Selection of Fields

LINQ allows developers to project only certain fields from a collection into a new collection. The code samples show how to extract book titles from a collection of books into a new IEnumerable<string> collection using both query syntax and lambda expressions. Any type can be used for the result collection, such as an anonymous type containing a book and its first author.

Uploaded by

jhansi rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Projection/Selection of Fields

LINQ allows developers to project only certain fields from a collection into a new collection. The code samples show how to extract book titles from a collection of books into a new IEnumerable<string> collection using both query syntax and lambda expressions. Any type can be used for the result collection, such as an anonymous type containing a book and its first author.

Uploaded by

jhansi rani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Projection/Selection of fields

Using LINQ, developers can transform a collection and create new collections where elements contain just some fields. The
following code shows how you can create a collection of book titles extracted from a collection of books.

Hide   Copy Code

Book[] books = SampleData.Books;


IEnumerable<string> titles = from b in books
select b.Title;

foreach (string t in titles)


Console.WriteLine("Book title - {0}", t);

As a result of this query, IEnumerable<string> is created because the type of expression in the select part is string. An
equivalent example written as a select function and lambda expression is shown in the following code:

Hide   Copy Code

Book[] books = SampleData.Books;


IEnumerable<string> titles = books.Select(b=>b.Title);

foreach (string t in titles)


Console.WriteLine("Book title - {0}", t);

Any type can be used as a result collection type. In the following example, an enumeration of anonymous classes is returned,
where each element in the enumeration has references to the book and the first author of the book:

You might also like