In the simplified code below,
if(city == "New York City")
{
var MyObject = from x in MyEFTable
where x.CostOfLiving == "VERY HIGH"
select x.*;
}
else
{
var MyObject = from x in MyEFTable
where x.CostOfLiving == "MODERATE"
select x.*;
}
foreach (var item in MyObject)
{
Console.WriteLine("<item's details>");
}
The variable MyObject is not accessible outside conditional block. How can I iterate outside the if..else ?
try this: