I have a LINQ query such as:
using(csv.......)
{
csv.Select(x => new Data() { ID = x[27], Raw = String.Join(",", x) });
}
//Do some stuff
//Now I want to use the result of the query
foreach(var item in ??)
{
}
The query returns IEnumerable within my using statement but how do I declare and then assign a variable so I can use it later on in a foreach statement?
The type of the query is
IEnumerable<Data>which you can declare outside theusingblock. However, from the comments it is obvious that you cannot enumerate aftermyObjecthas been disposed. If you don’t want to pull all elements into a list before leaving theusingblock your only option is to do the enumeration inside theusingblock. Pulling data from a database requires an open connection to that database etc.