Declaring an implicitly typed variable inside conditional scope and using it outside

前端 未结 7 2179
闹比i
闹比i 2021-01-06 01:51

In the simplified code below,

if(city == \"New York City\")
{
  var MyObject = from x in MyEFTable
                     where x.CostOfLiving == \"VERY HIGH\         


        
7条回答
  •  醉酒成梦
    2021-01-06 02:02

    try this:

    System.Linq.IQueryable MyObject = null;
    if(city == "New York City")
    {
      MyObject = from x in MyEFTable
                 where x.CostOfLiving == "VERY HIGH"
                 select x.*;
    }
    else
    {
      MyObject = from x in MyEFTable
                 where x.CostOfLiving == "MODERATE"
                 select x.*;
    }
    
    foreach (var item in MyObject)
    {
      Console.WriteLine("");
    }
    

提交回复
热议问题