Garima Goyal
Garima Goyal

Reputation: 1

Using IQueryable with and without AsQueryable()

I would like to know what happens when I use IQueryable with and without AsQueryable(). Here is an example:

public partial class Book
{
.......
public Nullable<System.DateTime> CheckoutDate{get; set;}

} 

I need to filter the data from SQL server before it is returned to an application server. I need to return books checked out more recently than entered date. Which one should I use?

A.

IQueryable<Book> books = db.Books;
books = books.Where(b => b.CheckoutDate >= date);

B.

IQueryable<Book> books = db.Books.ToList().AsQueryable();
books = books.Where(b => b.CheckoutDate >= date);

Basically I would like to know what is the difference between the above two options. Do they work on the similar grounds? Do they return same values?

Upvotes: 0

Views: 120

Answers (1)

Oscar
Oscar

Reputation: 13980

With B option, you're basically retrieving every book from database and filtering data in memory.

A option is more performance, as it filters data at the database and return only the rows that match your query.

Upvotes: 1

Related Questions