none
CTP5 EntityFramework drops the query result set while enumerating within foreach loop RRS feed

  • Question

  • Implementing Code First with two simple Entity
    
        public class Restaurant
        {
            public virtual int ID { get; set; }
            public virtual string Name { get; set; }
            public virtual Address Address { get; set; }
        }
    
        public class Address
        {
            public virtual int ID { get; set; }
            public virtual string City { get; set; }
        }
    
    And the Framework
    
     public class DB : System.Data.Entity.DbContext
     {
           public System.Data.Entity.DbSet<Restaurant> Restaurants { get; set; }
            public System.Data.Entity.DbSet<Address> Addresses { get; set; }
     }
    
    Database is properly populated
    
    Now I have a piece of LINQ code to review the data:
    
    DB _db = new DB()
    string x = "";
    var query = from r in _db.Restaurants
                       select r;
    
    foreach (var item in query)
           x += item.Name + " " + item.Address.City + "\n";
    
    Before foreach loop the ResultView conatins the query result. Once enumeration starts ResultView becomes empty. If you only use the following code then there is no problem
    
    foreach (var item in query)
           x += item.ID + " " + item.Name; + "\n";
    
    Anybody knows why? 
    


    ihe
    Thursday, November 17, 2011 10:11 PM

Answers

  • Hi Ihe,

    I test your code, but it works on my computer. I think your problem relates to "Lazy loading". You can try to add .Include("Address") method in your query:

     string x = "";
                    var query = from r in _db.Restaurants.Include("Address") select r;
                    foreach (var item in query)
                    x += item.Name + "_" + item.Address.City + "\n";
                    Console.WriteLine(x);
    

    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.

    Monday, November 21, 2011 2:09 AM
    Moderator

All replies

  • var query = from r in _db.Restaurants
                       select r;
    
    

     

    Seems to me... that the 'r' is one DbSet, however when you call the 'item.Address.City' it error or clears the result since it is non existant in the 'item'


    you might need to modify the above code to include join and select new
    • Edited by RoninB Thursday, November 17, 2011 10:35 PM
    Thursday, November 17, 2011 10:34 PM
  • Hi Ihe,

    I test your code, but it works on my computer. I think your problem relates to "Lazy loading". You can try to add .Include("Address") method in your query:

     string x = "";
                    var query = from r in _db.Restaurants.Include("Address") select r;
                    foreach (var item in query)
                    x += item.Name + "_" + item.Address.City + "\n";
                    Console.WriteLine(x);
    

    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.

    Monday, November 21, 2011 2:09 AM
    Moderator