简体   繁体   English

C#,如果否则然后用LINQ语句

[英]c# if else then statement with LINQ

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. if (result!= null)的结果下面有一条斜线,表明名称结果在当前上下文中不存在。

Not sure how to fix this. 不确定如何解决此问题。 I tried to initially var result = null but C# didn't like that. 我最初尝试var result = null但是C#不喜欢这样。

The variable is limited to the block it is declared in. So you have two result s, one in if block and one in else block which are different and are not visible outside their blocks. 该变量仅限于在其中声明的块。因此,您有两个result s,一个in if块和一个in else块,它们是不同的,并且在其块外不可见。

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. 您需要在块外定义变量,但是随后需要具体说明类型,因为C#编译器需要直接赋值表达式来推断var关键字的类型。

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. 另请注意, result永远不会为null ,因此,我相信我已将其替换为Any但我认为这不是问题的要旨。

You need to declare result before the if statement: 您需要在if语句之前声明result

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声明保留在另一个变体中:

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. 现在,您应该以某种方式找到一种方法来更改if或将其完全删除,因为在那个时候result不能为null

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())
    {
        ...
    }

You can perform the where separately from the main query: 您可以与主查询分开执行where:

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. 您需要在第一个if-else之前声明结果变量。

Also you need paranthesis around the condition in the second if statement. 另外,您还需要在第二个if语句中围绕条件进行套叠。

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. 第一次结果是一个int ,第二次是我声明一个string ,而第三次是未声明的result 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. 注意,使用您的代码,结果将始终为非空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM