在下面的简化代码中,
变量MyObject在条件块外不可访问。我该如何在if..else之外进行迭代?
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>");
}
变量MyObject在条件块外不可访问。我该如何在if..else之外进行迭代?
x.*
是指匿名类型的构造吗?如果不是,为什么要坚持隐式类型声明呢? - CodesInChaosselect new {columnA, columnB, columnC}
。对于过分简化的描述,我感到抱歉。 - FMFF