IEnumerable vs IQueriable RRS feed

  • Question

  • Hi  I am confused about when to use one in EF. I need simple explanation with a simple example/scenario.

    I found this while searching:

    IEnumerable<T> result= somequery

    IQueriable<T> result=the same exact query as above. 

    The first would load all data into memory at once. The second will create "stored procedure " in the database and records will only be fetched one by one like data reader. How can the query know if I am asking ienumerable or the other? 

    • Edited by kobosh3 Thursday, September 19, 2019 10:18 PM
    Thursday, September 19, 2019 10:16 PM


  • Hi kobosh3, 

    Thank you for posting here.

    For your question, you want to know the differences between IEnumerable and IQueriable.

    IEnumerable is a collection of objects in memory that you can enumerate and suitable for querying data from in-memory collections like List, Array.

    The query process for IEnumerable is as follows.

    IQueryable interface inherits from IEnumerable. IQueryable is suitable for querying data from out-memory (like remote database, service) collections.

    Calls made on IQueryable operate by building up the internal expression tree instead. These methods that extend IQueryable(Of T) do not perform any querying directly. Instead, their functionality is to build an Expression object, which is an expression tree that represents the cumulative query.

    it’s query process is as follows.

    IEnumerable’s filter logic executes on the client side and the IQueryable’s filter logic executes on the database.

    For more differences about IEnumerable and IQueriable, you can refer to the following reference.

    Returning IEnumerable<T> vs. IQueryable<T> [closed]

    Besides, here’s a reference about IQueryable vs IEnumerable in Entity Framework.

    Entity Framework: IQueryable vs. IEnumerable

    Hope them can help you.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by kobosh3 Friday, September 20, 2019 4:27 PM
    Friday, September 20, 2019 3:02 AM