Proper query to avoid overhead RRS feed

  • Question

  • Hi there,

    I`m migrating from Linq2Sql to EF4.1 and I have a doubt about how to load data from db.

    If I wanted to select a single row, which option would be best?

    dc.Groups.Where(group => group.Id == id).Load();
    return (from f in dc.Groups.Local
            select new SampleIntervalParameter.Factory
                LoadBaseEntity = f.GroupPeriod


    return (from f in dc.Groups.Local
            where f.Id == id
            select new SampleIntervalParameter.Factory
                LoadBaseEntity = f.GroupPeriod

    Are those two queries the same thing? I`m afraid to use one of them and load ALL the data from and the filter it.



    Wednesday, February 15, 2012 2:44 PM


All replies

  • I think your are a little bit confused by Linq2Sql.

    With EF you don't need to Load, if you want to select a record , you can simply do
    (assuming dc as the context and Groups as the "Table")

    return (dc.Groups.Single(g => g.Id == id));

    Wednesday, February 15, 2012 4:46 PM
  • Well, I was using Load() because there were a few queries that would not work without it..

    If I remember correctly, if I want all records, I have to use


    Can you explain when I do need to use Load() and when I dont?


    Wednesday, February 15, 2012 5:30 PM
  • I use EF since a lot of time and I never use the Load() method.

    But you are using EF or Linq2Sql ?

    To iterate through all the records you can

    var query = from g in dc.Groups select g;
    foreach (Group item in query.ToList())
        // do something


    IQueryable<Groups> query = from g in dc.Groups select g;
    foreach (Groups item in query.ToList<Groups>())
        // do something

    Wednesday, February 15, 2012 5:38 PM
  • I`m using EF4.1. The weird part is that I never had to use Load() with Linq2Sql. But when I started using EF, a few of my queries only worked if I called Load() first and then used Local to access it.

    Unfortunetly I cannot provide an exemple, since I`ve used Load() on all my methods, I do not remember which ones made me start thinking I needed it for all of them.

    Wednesday, February 15, 2012 5:43 PM
  • Which kind of EF approach do you use ? Code first, database first ?

    The Load method doesn't exist in EF Context neither in the entities collections...
    I think you are not using EF

    Do you have the .edmx model and succesfully create the context ?

    Wednesday, February 15, 2012 5:49 PM
  • Have you ever used EF 4.1? It does not exits in the version of EF that comes with VS. You have to install it separetly.

    Also, you need to import System.Data.Entity to use Load() method and Local property :)

    Wednesday, February 15, 2012 5:57 PM
  • Yes guy, I use EF 4.1 and I know it fairly good, but the Load method doesn't exist in context or entities.

    I'm pretty sure. I use EF since it first release...

    Wednesday, February 15, 2012 6:03 PM
  • Well, there must be two different ways to use it or idk... I gotta go study the correct use of that method to avoid using it wrong.
    Wednesday, February 15, 2012 6:08 PM
  • Hi Lucas Phillip,

    Welcome to MSDN Forum.

    I think you are talking about explicitly loading related entity objects, here's some links I think can help you with Load method.

    Loading Related Objects

    How to: Explicitly Load Related Objects

    EntityCollection(Of TEntity).Load Method (MergeOption)

    Using DbContext in EF 4.1 Part 11: Load and AsNoTracking

    Based on the post you followed, I also find you mentioned "Local". The Local property of DbSet provides simple access to the entities of the set that are currently being tracked by the context and have not been marked as Deleted. Accessing the Local property never causes a query to be sent to the database. This means that it is usually used after a query has already been performed. The Load extension method can be used to execute a query so that the context tracks the results.

    Here's an article from team blog which I think can help you.

    Using DbContext in EF 4.1 Part 7: Local Data

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Lucas Phillip Friday, February 17, 2012 11:06 AM
    Friday, February 17, 2012 1:49 AM
  • Thank you :)
    Friday, February 17, 2012 11:06 AM