Tag: conditional statements

在条件范围内声明隐式类型变量并在外部使用它

在下面的简化代码中, 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(“”); } 在条件块之外无法访问变量MyObject。 我怎样才能在if..else之外迭代?