This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, We have a function that returns a IEnumerable type. The function is in charge of creating the unit of work at the beginning and disposing it at the end using the 'using' keyword. I call the AsEnumerable method extension to return the list.
The problem is that the caller of this function receives a IEnumerable type but immeditaly when I try to loop through items, I receive an error because the unit of work is disposed. If I call the ToList method extension, I don't have any problem because the query to the database is immediatly executed and it's a good pattern. In my case, I absolutly want the function returning a IEnumerable because I don't want user of the API to be able to add new items like it is possible with IList. I can call the ToList method followed by AsEnumerable but I think it will be an overhead and I'm sure you have a much better approach. Have you any other solution? Thank you very much. |
|
|
I would enumerate the query with prior to returning, otherwise your using statement is disposing of your unit of work prematurely. Jeff
|
|
|
Thanx Jeff for replying I know that I can call a ToList like I said in my question but I think there will be an overhead of performance to convert the list from IList to IEnumerable. |
|
|
Other than a type cast, why would you think that? IList implements IEnumerable, so if you want to guarantee there is not a performance hit, then just cast to IEnumerable. The bigger performance hit is the enumeration of the query, which since you are within a using block in your method is not avoidable. Jeff |
|
|
Hi Jeff, Thank you again for the fast replying! :) I just looked to the code of the Enumerable type and you're right; the function AsEnumerable don't do any extra processing to convert the IList to IEnumerable:
Thank you. |
|