0% found this document useful (0 votes)
34 views

Basic Query: Ienumerable Book

The document provides an example of a basic LINQ query. It shows querying a collection of books by specifying the collection to query ("from b in books") and the data to select ("select b"). This returns an IEnumerable of Book objects. The query simply selects all books without filtering, demonstrating the basic structure of a LINQ query. The document then explains that more useful queries will be shown in following examples, such as projecting/selecting only certain fields from the collection.

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)
34 views

Basic Query: Ienumerable Book

The document provides an example of a basic LINQ query. It shows querying a collection of books by specifying the collection to query ("from b in books") and the data to select ("select b"). This returns an IEnumerable of Book objects. The query simply selects all books without filtering, demonstrating the basic structure of a LINQ query. The document then explains that more useful queries will be shown in following examples, such as projecting/selecting only certain fields from the collection.

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

Basic query

The following example shows the basic usage of LINQ. In order to use a LINQ to Entities query, you will need to have a
collection (e.g., array of books) that will be queried. In this basic example, you need to specify what collection will be queried
("from <<element>> in <<collection>>" part) and what data will be selected in the query ("select <<expression>>" part). In
the example below, the query is executed against a books array, book entities are selected, and returned as result of queries.
The result of the query is IEnumerable<Book> because the type of the expression in the "'select << expression>>" part is
the class Book.

Hide   Copy Code

Book[] books = SampleData.Books;


IEnumerable<Book> bookCollection = from b in books
select b;

foreach (Book book in bookCollection )


Console.WriteLine("Book - {0}", book.Title);

As you might have noticed, this query does nothing useful - it just selects all books from the book collection and puts them
in the enumeration. However, it shows the basic usage of the LINQ queries. In the following examples you can find more
useful queries.

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.

You might also like