none
How does Linq to Sql retrieve data RRS feed

  • Question

  • Hi all I am interested in using Linq to SQL but am not sure that the application that I have is the best choice for this application.  I basically have a SQL Agent job that manipulates and moves a lot of data around.  I have created a couple of console apps to use as steps in this application.  Previously I was using ADO .Net to get the job done but really love using Linq so I thought I would give Linq to SQL a try. 

    One of my concerns is that I often do not need to grab all the data in a table so I am not quite sure how Linq to SQL works at retrieving data.  If I get a table but filter out certain rows via Linq where clause is Linq to SQL smart enough to not grab everything in the table bring all the data into my client application and then apply the filter after the fact?  I am hoping that it looks at the filtering and correspondingly gets only the necessary data from the server instead of everything in the data table.  Is this the case?

     


    Thanks, -ja
    Friday, November 11, 2011 12:21 AM

Answers

  • Hi Ja,

    Welcome!

    This is difference between IQueryable and IEnumerable, you can refer here: http://blogs.msdn.com/b/wriju/archive/2008/05/06/linq-ienumerable-t-and-iqueryable-t.aspx

    In LINQ to SQL, the LINQ query will be transalted by LINQ Provider to "T-SQL", you can use SQL Profiler to watch them.

    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.

    Friday, November 11, 2011 3:33 AM
    Moderator
  • Hi John;

    It's all in the way you formulate your query. For example the following query is based on the Microsoft AdventureWorks sample database.

    This query will do the filtering om the server:

     

    // This query will get all the married employees and do the filtering om the server:
    var results = from ms in DataContextName.Employees
    	where ms.MaritalStatus == 'M'
    	select ms;
                  
    // This query will get all the married employees and do the filtering om the local system:
    var results = (from ms in DataContextName.Employees              
                  select ms).ToList().Where( ms => ms.MaritalStatus == 'M');
    

    Note where the where clause is in both queries.

     

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, November 11, 2011 3:13 PM

All replies

  • Hi Ja,

    Welcome!

    This is difference between IQueryable and IEnumerable, you can refer here: http://blogs.msdn.com/b/wriju/archive/2008/05/06/linq-ienumerable-t-and-iqueryable-t.aspx

    In LINQ to SQL, the LINQ query will be transalted by LINQ Provider to "T-SQL", you can use SQL Profiler to watch them.

    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.

    Friday, November 11, 2011 3:33 AM
    Moderator
  • Hi John;

    It's all in the way you formulate your query. For example the following query is based on the Microsoft AdventureWorks sample database.

    This query will do the filtering om the server:

     

    // This query will get all the married employees and do the filtering om the server:
    var results = from ms in DataContextName.Employees
    	where ms.MaritalStatus == 'M'
    	select ms;
                  
    // This query will get all the married employees and do the filtering om the local system:
    var results = (from ms in DataContextName.Employees              
                  select ms).ToList().Where( ms => ms.MaritalStatus == 'M');
    

    Note where the where clause is in both queries.

     

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, November 11, 2011 3:13 PM