pgrammer
May 4th, 2009, 11:08 AM
Hello,
I have a issue with a Linq query return falling out of scope. I am using the "using" block when initializing the data context so that it is disposed correctly. However, the "using" block is causing another issue down the road.
Example Code: Using a Stored Procedure
public object SelectAllEmployees()
{
using(DatabaseContext dbContext = new DatabaseContext())
{
var assocQuery = dbContext.SelectAllAssociates // accessing stored procedure.
return assocQuery
}
}
Now, I have done this exact thing with other objects that need disposing, but for some reason when using it with Linq it causes assocQuery to fall out of scope before the value is returned. I can fix the code by using the following example:
public object SelectAllEmployees()
{
var assocQuery = dbContext.SelectAllAssociates // accessing stored procedure.
return assocQuery
}
But the problem is this object doesn't appear to be disposed correctly, and my software runs for months without being shutdown, and sometimes these are causing issues. I need them disposed correctly. I tried to Dispose() it manually, however, I cannot place any statements after the "return" statement.
I guess this happens because assocQuery still has a connection to the database context even after the query has been run. Because this code will work normally with other objects that need disposing. Once the database context falls out of scope it is impossible to return the contents of assocQuery.
The second part of this is, that I would like to return a DataSet instead of a generic object. The return value of this function is being injected directly into a DataGridView and I think it's a bad idea to use "object". I have been trying for a few days to convert assocQuery to a DataSet and found many examples online, but none of them work correctly when I use them. I see that assocQuery has many properties like ".Cast<>" but I can't find anything that will convert it to a DataSet. It seems like a pretty basic operation. If I could convert this to a DataSet it would also solve my original problem, of returning a null reference. I could use the "using" block normally writting casting the contents to a DataSet, and returning the contents of the DataSet allowing the database context to dispose normally.
Otherwise, I think I need to make a copy of the data contained within assocQuery to another object that doesn't require disposing, without using a reference copy. I'm not sure how to go about doing that. I thought I would use the "&" operator or similar, but that may have been C++.
Any ideas?
I have a issue with a Linq query return falling out of scope. I am using the "using" block when initializing the data context so that it is disposed correctly. However, the "using" block is causing another issue down the road.
Example Code: Using a Stored Procedure
public object SelectAllEmployees()
{
using(DatabaseContext dbContext = new DatabaseContext())
{
var assocQuery = dbContext.SelectAllAssociates // accessing stored procedure.
return assocQuery
}
}
Now, I have done this exact thing with other objects that need disposing, but for some reason when using it with Linq it causes assocQuery to fall out of scope before the value is returned. I can fix the code by using the following example:
public object SelectAllEmployees()
{
var assocQuery = dbContext.SelectAllAssociates // accessing stored procedure.
return assocQuery
}
But the problem is this object doesn't appear to be disposed correctly, and my software runs for months without being shutdown, and sometimes these are causing issues. I need them disposed correctly. I tried to Dispose() it manually, however, I cannot place any statements after the "return" statement.
I guess this happens because assocQuery still has a connection to the database context even after the query has been run. Because this code will work normally with other objects that need disposing. Once the database context falls out of scope it is impossible to return the contents of assocQuery.
The second part of this is, that I would like to return a DataSet instead of a generic object. The return value of this function is being injected directly into a DataGridView and I think it's a bad idea to use "object". I have been trying for a few days to convert assocQuery to a DataSet and found many examples online, but none of them work correctly when I use them. I see that assocQuery has many properties like ".Cast<>" but I can't find anything that will convert it to a DataSet. It seems like a pretty basic operation. If I could convert this to a DataSet it would also solve my original problem, of returning a null reference. I could use the "using" block normally writting casting the contents to a DataSet, and returning the contents of the DataSet allowing the database context to dispose normally.
Otherwise, I think I need to make a copy of the data contained within assocQuery to another object that doesn't require disposing, without using a reference copy. I'm not sure how to go about doing that. I thought I would use the "&" operator or similar, but that may have been C++.
Any ideas?