none
Isn't Linq.Table<TEntity> very bad if the table is large? RRS feed

  • Question

  • I'm looking over a online store and in the sqlrepository there is a constructor that calls:

    productsTable = (new DataContext(connectionString)).GetTable<Product>();

    where productsTable is:

    private Table<Product> productsTable; //Product is a table in the db and a class in c# that represents that table

    from Linq.Table<TEntity>

    My question is - isn't calling this and getting the entire table to work with very bad?

    GetTable<Product>() returns the entire table from the db and if the table is large then it will take forever for the page to load right? Or am I missing something like GetTable only fetching the info from the table when asked because its a linq query?


    chuckdawit
    Tuesday, December 7, 2010 12:07 AM

Answers

  • You are right. As you said, this won't load the entire table into memory unless you call productsTable.ToArray()


    No olvides marcar la respuesta si te ha ayudado! http://bmegias.wordpress.com http://twitter.com/bmegias
    • Marked as answer by witdaj Monday, December 13, 2010 6:57 PM
    Tuesday, December 7, 2010 11:02 PM
  • It will fetch only the requested results.

    For example, given that LINQ statement

    var product = productsTable.single(p => p.code == 'PROD001');
    

    the underlying SQL query that gets executed is something similar to

    SELECT * FROM products WHERE code = 'PROD001'
    


    Please vote the answer if it was helpful!
    Por favor vota la respuesta si te ha ayudado!
    http://bmegias.wordpress.com
    http://twitter.com/bmegias
    • Marked as answer by witdaj Monday, December 13, 2010 6:57 PM
    Thursday, December 9, 2010 12:04 AM

All replies

  • You are right. As you said, this won't load the entire table into memory unless you call productsTable.ToArray()


    No olvides marcar la respuesta si te ha ayudado! http://bmegias.wordpress.com http://twitter.com/bmegias
    • Marked as answer by witdaj Monday, December 13, 2010 6:57 PM
    Tuesday, December 7, 2010 11:02 PM
  • If I need to pull info from 'productsTable' even if its just one product, will the linq go and fetch the entire products table or will it run a linq query to just fetch the results I'm looking for?
    chuckdawit
    Wednesday, December 8, 2010 10:58 PM
  • It will fetch only the requested results.

    For example, given that LINQ statement

    var product = productsTable.single(p => p.code == 'PROD001');
    

    the underlying SQL query that gets executed is something similar to

    SELECT * FROM products WHERE code = 'PROD001'
    


    Please vote the answer if it was helpful!
    Por favor vota la respuesta si te ha ayudado!
    http://bmegias.wordpress.com
    http://twitter.com/bmegias
    • Marked as answer by witdaj Monday, December 13, 2010 6:57 PM
    Thursday, December 9, 2010 12:04 AM