none
Finding a Specific object from an already populated EntitySet RRS feed

  • Question

  • This question may have been cross-posted.  Sorry about that.

    I have a query as follows: This is not my exact query.  But to make the code concise, I kinda made up a pseudo situation.

    IQueryable<Customers> custs = (from c in dnContext.Customers select c);
    foreach (Customer cust in custs)
    {
       if (cust.Name == "Somebody" )
             cust.Region = "NC";
    }
    At this point my 'custs' EntitySet is fully populated and also I have changed the 'Region' property of 'Somebody' to 'NC'. The original value was 'SC'.

    I have not called the SubmitChanges() yet. A few more lines of code later, my business logic requires to find some particular values from this entity set and take some actions on them.  Example, I need to find 'Somebody' whose 'Region' property has been modified.  So, I issue a statement as follows:

    Customer SomeCustomer = custs.FirstOrDefault( c => c.Name == "Somebody");

    But the 'Region' for 'SomeCustomer' still holds 'SC'.  I looked at the SQL statement generated by the above query and it seems that it is hitting the Database again to get the 'SomeCustomer' entity and since the SubmitChanges() has not been applied yet, it returns the unmodified Entity.

    So, my question is, How can I query the EntitySet Collection (not the database) to find a specific Entity?  Any sample code is highly appreciated.

    Babu.

    Tuesday, April 21, 2009 12:37 PM

Answers

  • Your 'custs' variable is not an EntitySet it is a query.  Each time you enumerate it you are going to execute a query against the database.  You can turn custs into a local collection easily with ToList.

    List<Customers> custs = (from c in dnContext.Customers select c).ToList();


    Also, normally L2S will give you back the same instance each time the databse is queried.  If you ran the query twice and the second time your retrieved the customer w/ name "Somebody" and it was not the same instance (since it did not have your change to the Region property) then something strange is going on.  It is possible you disabled object tracking, which would cause you to get new object instances for each query, but then you would have disabled your ability to do updates.


    Wayward LINQ Lacky
    Thursday, April 23, 2009 11:03 PM
    Moderator

All replies

  • Is there another linq datacontext being used? If there is, using different one will not have the changes of the original.
    William Wegerson (www.OmegaCoder.Com)
    Thursday, April 23, 2009 7:50 PM
    Moderator
  • Your 'custs' variable is not an EntitySet it is a query.  Each time you enumerate it you are going to execute a query against the database.  You can turn custs into a local collection easily with ToList.

    List<Customers> custs = (from c in dnContext.Customers select c).ToList();


    Also, normally L2S will give you back the same instance each time the databse is queried.  If you ran the query twice and the second time your retrieved the customer w/ name "Somebody" and it was not the same instance (since it did not have your change to the Region property) then something strange is going on.  It is possible you disabled object tracking, which would cause you to get new object instances for each query, but then you would have disabled your ability to do updates.


    Wayward LINQ Lacky
    Thursday, April 23, 2009 11:03 PM
    Moderator
  • Thanks Matt.  That exactly was the problem.  Once I changed my changed my query from IQueryable to IEnumerable, things are working fine.

    Babu.
    Friday, April 24, 2009 1:25 PM