Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does AsQueryable() on ICollection really makes lazy execution?

I am using Entity Framework CodeFirst where I have used Parent Child relations using ICollection as

public class Person
{
   public string UserName { get;set}
   public ICollection<Blog> Blogs { get; set;}
}

public class Blog
{
   public int id { get; set; }
   public string Subject { get; set; }
   public string Body { get; set; }
}

Ok, so far everything is working ok, but my concern is, whenever I want to get the Blogs of a person, I get it as

var thePerson = _context.Persons.Where(x => x.UserName = 'xxx').SingleOrDefault();
var theBlogs = thePerson.Blogs.OrderBy(id).Take(5);

Now, I understand that, when the line is executed, all Blogs for that person is loaded into the memory and then sorting and selecting is done from memory. That is not ideal for a record of Person who has large number of blogs. I want to make the Blog Child as IQueryable so that the Sorting and Selecting is done in SQL database before pulling to Memory.

I know I could declare the Blogs as IQueryable in my context so that I could directly query as

var theBlogs = _context.Blogs.Where(.....)

but that is not feasible for me due to design choice, I want to avoid any circular reference as much as possible due to serialization problem. So, I did not make any reference of the parent entity in my child.

I found that, i can call AsQueryable() method on the blogs as

var theBlogs = thePerson.Blogs.AsQueryable().OrderBy(id).Take(5);

That looks like a magic for me and seems too good to be true. So my question. Does this AsQueryable really make the ICollection as IQueryable in reality and makes all Query process in SQL Server (Lazy loading) OR it is just a casting where Blogs are loaded into memory as like before, but change the interface from ICollection to IQueryable ?

like image 638
Emran Hussain Avatar asked Feb 08 '12 10:02

Emran Hussain


People also ask

Why do we use AsQueryable () on collection?

AsQueryable is used when expression tree is to be constructed. I can think of scenario where it is best fit. In your example let say you require some information from database based on student ID. Now student is in memory collection.

What is the difference between AsEnumerable and AsQueryable?

AsEnumerable preserves deferred execution and does not build an often useless intermediate list. On the other hand, when forced execution of a LINQ query is desired, ToList can be a way to do that. AsQueryable can be used to make an enumerable collection accept expressions in LINQ statements.

What is the use of AsQueryable in LINQ?

AsQueryable() method is used to get an IQueryable reference. Let us see an example to find sum of integer values. Firstly, set an integer array. var arr = new int[] { 100, 200, 300, 400 };

Is IQueryable faster than IEnumerable?

IQueryable is faster than IEnumerable. In addition to Munesh Sharma's answer:IEnumerable loads data in-memory and then apply filters to it one by one but IQueryable apply filters all at once and return the result.


2 Answers

So actually it appears that writing your navigation property as IQueryable<T> is not possible.

What you could do is adding a navigation property to Blog:

public class Blog
{
   public int id { get; set; }
   public string Subject { get; set; }
   public string Body { get; set; }
   public virtual Person Owner { get; set; }
}

From that, you can query as follows so it won't load everything into memory:

var thePerson = _context.Persons.Where(x => x.UserName = 'xxx').SingleOrDefault();
var results = _context.Blogs.Where(z => z.Person.Name = thePerson.Name).OrderBy(id).Take(5)

I suggest you to try LINQPad to see how LINQ is translated into SQL, and what is actually requested from the DB.

like image 83
ken2k Avatar answered Sep 21 '22 12:09

ken2k


A better approach is described in Ladislav's answer. In your case:

var theBlogs = _context.Entry(thePerson)
                       .Collection(x => x.Blogs)
                       .Query()
                       .OrderBy(x => x.id)
                       .Take(5);
like image 45
Diego Mijelshon Avatar answered Sep 18 '22 12:09

Diego Mijelshon