Imagine you have the following code:
public static IEnumerable<Product> GetAllProducts()
{
using (AdventureWorksEntities db = new AdventureWorksEntities())
{
var products = from product in db.Product
select product;
foreach (Product product in products)
{
yield return product;
}
}
}
Then in some other place in your code you have the following:
foreach(var p in GetAllProducts())
{
FunctionWhichProcessProductForTenSeconds(p);
FunctionWhichProcessProductForTwentySeconds(p);
}
does it mean that during all period of execution of foreach(var p in GetAllProducts()) connection to database will remain open?