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

Ienumerable Book: Int in

LINQ allows developers to filter collections and create new collections containing only entities that meet certain conditions, such as books with "our" in the title and a price less than $500. The code samples demonstrate filtering a collection of books to return only those matching the criteria, using either query syntax with a where clause or the Where() method. The filtered collection is of type IEnumerable<Book> since it contains Book objects selected from the original collection.

Uploaded by

jhansi rani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Ienumerable Book: Int in

LINQ allows developers to filter collections and create new collections containing only entities that meet certain conditions, such as books with "our" in the title and a price less than $500. The code samples demonstrate filtering a collection of books to return only those matching the criteria, using either query syntax with a where clause or the Where() method. The filtered collection is of type IEnumerable<Book> since it contains Book objects selected from the original collection.

Uploaded by

jhansi rani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Using LINQ, developers can filter entities from a collection and create a new collection containing just entities

that satisfy a
certain condition. The following example creates a collection of books containing the word "our" in the title with price less
than 500. From the array of books are selected records whose title contains the word "our", price is compared with 500, and
these books are selected and returned as members of a new collection. In ''where <<expression>>'' can be used a valid C#
boolean expression that uses the fields in a collection, constants, and variables in a scope (i.e., price). The type of the
returned collection is IEnumerable<Book> because in the ''select <<expression>>'' part is the selected type Book.

Hide   Copy Code

Book[] books = SampleData.Books;


int price = 500;
IEnumerable<Book> filteredBooks = from b in books
where b.Title.Contains("our") && b.Price < price
select b;

foreach (Book book in filteredBooks)


Console.WriteLine("Book - {0},\t Price {1}", book.Title, book.Price);

As an alternative, the .Where() function can be used as shown in the following example:

Hide   Copy Code

Book[] books = SampleData.Books;


int price = 500;
IEnumerable<Book> filteredBooks = books.Where(b=> (b.Title.Contains("our")
&& b.Price < price) );

foreach (Book book in filteredBooks)


Console.WriteLine("Book - {0},\t Price {1}", book.Title, book.Price);

You might also like