Question

I have the following code:

if (intval == 0)
{
    var result = (from dm in datacontext.Trk
                  where dm.ID == 0
                  select dm);
}
else
{
   var result = (from dm in datacontext.Trk
                 where dm.ID != 0
                 select dm);
}

if (result != null)
{
    // do something
}

There is a swigly line under the result in if (result!= null) saying that the name result does not exist in the current context.

Not sure how to fix this. I tried to initially var result = null but C# didn't like that.

Was it helpful?

Solution

how about something like this:

    var result = 
        intval == 0 
            ?(from dm in datacontext.Trk where dm.ID = 0 select dm) 
            :(from dm in datacontext.Trk where dm.ID != 0 select dm);

    if(results.Any())
    {
        ...
    }

OTHER TIPS

The variable is limited to the block it is declared in. So you have two results, one in if block and one in else block which are different and are not visible outside their blocks.

You need to define the variable out of blocks, but then you need to be specific about the type, because C# compiler needs direct assignment expression to infer the type for var keyword.

However, I suggest rethinking your code and doing somthing like following:

var result = from dm in datacontext.Trk
             where ((intval == 0) ? dm.ID == 0 : dm.ID != 0)
             select dm;

if (result.Any())
{
   // do something
}

Please also note that result will never be null, therefore, I have replaced it with Any but it is not a gist of the question, I believe.

You need to declare result before the if statement:

object result = null;

if (intval = 0)
{
   result = (from dm in datacontext.Trk
                 where dm.ID = 0
                 select dm);

}
else
{
   result = (from dm in datacontext.Trk
                 where dm.ID != 0
                 select dm);


 }

 if (result != null)
 {

  // do something

 }

Or you can keep the var declaration in this other variant:

var result = (from dm in datacontext.Trk
                 where dm.ID != 0
                 select dm);

if (intval = 0)
{
   result = (from dm in datacontext.Trk
                 where dm.ID = 0
                 select dm);

}

if (result != null)
{
   // do something

}

Now you should somehow find a way to either change that if or remove it completely because result cannot be null at that point.

You can perform the where separately from the main query:

var result = from dm in datacontext.Trk select dm;
if (intval == 0)
{
    result = result.Where(dm => dm.ID == 0);
}
else
{
    result = result.Where(dm => dm.ID != 0);
}

if (result.Any())
{
    // do something
}

You need to declare the result variable before the first if-else.

Also you need paranthesis around the condition in the second if statement.

This code would also cause the problem:

if (value == 0)
{
     int result = 1;
}
else
{
     string result = "testing";
}

if (result != 1)
{
     // do something
} 

The first time result is an int, the second time I declare a string, and the third time result is undeclared. The reason that they can have different types is because the first two declarations belong to different scopes. Each { ... } gets its own scope.

If you want to share one variable between scopes, you'll need to declare it outside. But now, since the same variable is used in all three places, there is a compiler error that the types don't match:

int result;

if (value == 0)
{
    result = 1;
}
else
{
    result = "testing"; // type error here
}

if (result != 1)
{
     // do something
} 
List<TypeOfDm> dmList; // <=== Declare dmList outside of block statements.
if (intval == 0) { // <=== Use "==" for comparision, "=" is assignement.
    dmList = datacontext.Trk
        .Where(dm =>  dm.ID == 0)
        .ToList();
} else { 
    dmList = datacontext.Trk
        .Where(dm =>  dm.ID != 0)
        .ToList();
} 
if (dmList.Count != 0) { 
  // do something 
} 

Note, with your code your result will always be non-null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top