none
EF Lazy loading and queries RRS feed

  • Question

  • Hello.

    For example, I have an entity User.

    public class User
    {
        public int Id  {get; set; }
    }


    And I have a context of database, which knows about User:

    public DbSet<User> Users { get; set; }


    Now I have a repository class:

    public class Repository {

    private DbContext _context;

    Repository()
    {

    _context = new MyContext();

    }
    IEnumerable<User> All()
    {
    _context.Users;
    } }

    This is a very-very simple example, i am using architecture rules :)

    So a question: If I will call in program such code:

    Repository rep = new Repository();
    
    rep.All().Count();

    EF will download all rows from table and call Count() or will generate COUNT Function of SQL?
    I know about IQueryable, i want to use IEnumerable.

    Sorry for my English.



    • Edited by ArtsiomRusak Wednesday, November 27, 2013 3:43 PM
    Wednesday, November 27, 2013 3:41 PM

Answers

  • Your DbContext already is a Repository.  There is no need to wrap it in another Repository. 

    And you are starting to see why it's not only useless, but harmful to try.

    >I know about IQueryable, i want to use IEnumerable.

    Don't do that.  Use the tools like they are intended. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by ArtsiomRusak Thursday, November 28, 2013 3:16 PM
    Wednesday, November 27, 2013 6:21 PM
  • Hello,

    >>EF will download all rows from table and call Count() or will generate COUNT Function of SQL?

    Since the return type is IEnumerable, it will first download all rows from table and call Count().

    This is because the difference between IEnumerable and IQueryable.

    While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.

    While query data from database, IQueryable execute select query on server side with all filters.

    More details, see here.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ArtsiomRusak Thursday, November 28, 2013 3:16 PM
    Thursday, November 28, 2013 6:58 AM
    Moderator

All replies

  • Your DbContext already is a Repository.  There is no need to wrap it in another Repository. 

    And you are starting to see why it's not only useless, but harmful to try.

    >I know about IQueryable, i want to use IEnumerable.

    Don't do that.  Use the tools like they are intended. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Marked as answer by ArtsiomRusak Thursday, November 28, 2013 3:16 PM
    Wednesday, November 27, 2013 6:21 PM
  • Hello,

    >>EF will download all rows from table and call Count() or will generate COUNT Function of SQL?

    Since the return type is IEnumerable, it will first download all rows from table and call Count().

    This is because the difference between IEnumerable and IQueryable.

    While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.

    While query data from database, IQueryable execute select query on server side with all filters.

    More details, see here.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ArtsiomRusak Thursday, November 28, 2013 3:16 PM
    Thursday, November 28, 2013 6:58 AM
    Moderator