none
When do we use IQueryable? or IEnumerable? or ToList()? RRS feed

  • Question

  • Man, is this EF a black box or what? :-) long learning curve but here we go....

    I ma having difficulty to find out the best practices to create my classes. So far I know I can create Repositories for them.
    Some people create a General repository and then derive classes from it...anyways I've noticed that people create stuff like this:

        public interface IStudentRepository : IDisposable
        {
            IEnumerable<Student> GetStudents();
         }
    
            public IEnumerable<Student> GetStudents()
            {
                return context.Students.ToList();
            }
    
    so here we have IEnumerable and ToList()
    

    or we can have:

     

        public interface IComponentRepository
        {
            IQueryable<Component> All { get; }
        }
    ...
            public IQueryable<Component> All
            {
                get { return context.Components; }
            }
    
    IQueryable and no ToList()
    

    When do we use IQueryable? or IEnumerable? or ToList()? this new sintaxe is so confusing :)

     

    Thanks


    Max
    Monday, October 17, 2011 11:16 PM

Answers

  • Hi Max,

    Welcome!

    I think you want to know the difference between Iqueryable and IEnumerable, you can refer this link:

    http://blogs.msdn.com/b/erickt/archive/2006/10/23/iqueryable-t-vs-ienumerable-t.aspx

    http://stackoverflow.com/questions/252785/what-is-the-difference-between-iqueryablet-and-ienumerablet

    My understanding is : the IEnmerable is in memory, Iqueryable doesn't.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Maximusdm Tuesday, October 18, 2011 4:50 PM
    Tuesday, October 18, 2011 8:52 AM
    Moderator
  • IQueryable returns a "queryable" that is a query you could still be enriched before really sending it to the server.

    IEnumerable returns a list that is the actual querying took place and you get the results. ToList is isued to force running the query and returning these enumerable results...

    So in short :
    - use IQueryable if you want to return a base query that could be further enhanced before running it server side (by enumerating its items)..
    - use IEnumerable/ToList if you want to return a list that has been retrieved from the db

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Holger Kreissl Tuesday, October 18, 2011 12:29 PM
    • Marked as answer by Maximusdm Tuesday, October 18, 2011 4:50 PM
    Tuesday, October 18, 2011 10:05 AM
  • For .ToList(), it'll force the code to actually fetch data and stop being lazy, so it's helpful to call this method before you start to loop data.

    In general, for flexability's sake I'll want all these queries defined to return IQueryable<> so you can append it with other modifier methods like .Count() as appropiate.

    • Proposed as answer by Holger Kreissl Tuesday, October 18, 2011 12:29 PM
    • Marked as answer by Maximusdm Tuesday, October 18, 2011 4:50 PM
    Tuesday, October 18, 2011 10:06 AM

All replies

  • Hi Max,

    Welcome!

    I think you want to know the difference between Iqueryable and IEnumerable, you can refer this link:

    http://blogs.msdn.com/b/erickt/archive/2006/10/23/iqueryable-t-vs-ienumerable-t.aspx

    http://stackoverflow.com/questions/252785/what-is-the-difference-between-iqueryablet-and-ienumerablet

    My understanding is : the IEnmerable is in memory, Iqueryable doesn't.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Maximusdm Tuesday, October 18, 2011 4:50 PM
    Tuesday, October 18, 2011 8:52 AM
    Moderator
  • IQueryable returns a "queryable" that is a query you could still be enriched before really sending it to the server.

    IEnumerable returns a list that is the actual querying took place and you get the results. ToList is isued to force running the query and returning these enumerable results...

    So in short :
    - use IQueryable if you want to return a base query that could be further enhanced before running it server side (by enumerating its items)..
    - use IEnumerable/ToList if you want to return a list that has been retrieved from the db

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Proposed as answer by Holger Kreissl Tuesday, October 18, 2011 12:29 PM
    • Marked as answer by Maximusdm Tuesday, October 18, 2011 4:50 PM
    Tuesday, October 18, 2011 10:05 AM
  • For .ToList(), it'll force the code to actually fetch data and stop being lazy, so it's helpful to call this method before you start to loop data.

    In general, for flexability's sake I'll want all these queries defined to return IQueryable<> so you can append it with other modifier methods like .Count() as appropiate.

    • Proposed as answer by Holger Kreissl Tuesday, October 18, 2011 12:29 PM
    • Marked as answer by Maximusdm Tuesday, October 18, 2011 4:50 PM
    Tuesday, October 18, 2011 10:06 AM
  • I know this is a bit late but Count() can be used with IEnumerable. If you want to read a highly scrutinized article written article about how IQueryable<T> is only good when it is used in it is in-memory (part of a Base Class Model or ViewModel) read this.  I learned more about its effectiveness from the commenters than I did the author. IMO, IQueryable<T> is how you want to interface with the data layer whether it is via service or thru Web app. I think cheong00 is on to something about the use of in-memory with IEnumerable<T>. My guess is that if I have a query that is compiling a static list globally I would use IEnumerable. If my query was user specific or needed to be extended I would use IQueryable. With EF it is likely that we use LINQ most of the time so when in doubt use IQueryable?
    Tuesday, December 3, 2013 1:51 PM
  • You got it. Especially on the "needed to be extended" part. You'll find it useful if your program's logic shapes like "look for the record count of something without actually retrieve it, then optionally return page X of Y rows pages", where you'll want to plug some.Count, .Skip() and .Take() in.

    So while in the document suggest the providing parameter of type IQueryable<> is bad, I feel there's nothing wrong to write data repository methods that returns them. If your data set is big, writting code in this way will save you lots of memory.

    Wednesday, December 4, 2013 6:04 AM