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

前端 未结 7 2156
闹比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:04

    you'll have to define the MyObject as a var before the condition:

    var MyObject = from x in MyEFTable
                         where x.CostOfLiving == "SOMETHING THAT'LL RETURN NO ROWS"
                         select x.*;
    

    This will assign a schema to the MyObject variable.

    Now you can proceed with your condition as:

    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.*;
    
    }
    

提交回复
热议问题