Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中LINQ中的多个If-else条件#_C#_Linq - Fatal编程技术网

C# C中LINQ中的多个If-else条件#

C# C中LINQ中的多个If-else条件#,c#,linq,C#,Linq,我有一个linq查询,如果p.conditionVariable>0我将应用以下条件,我需要在其中设置条件 from prob in table2.where(p => p.Id == p.ConditionVariable && !p.IsBlocked && p.IsActive) 如果p.conditionVariable==0,则以下内容保持不变 (from _obj1 in table1.Where(p => p.IsActive == t

我有一个linq查询,如果
p.conditionVariable>0
我将应用以下条件,我需要在其中设置条件

from prob in table2.where(p => p.Id == p.ConditionVariable && !p.IsBlocked && p.IsActive)
如果p.conditionVariable==0,则以下内容保持不变

(from _obj1 in table1.Where(p => p.IsActive == true))
                           from prob in table2.Where(p => p.Id == _obj1.Id && !p.IsBlocked && p.IsActive && p.ConditionVariable == 0)
                           select new Class1
                           {
                               Title = prob.Title,
                               Status = prob.IsPending,
                               Id = obj1.id 
                           }

我想您需要在条件之间创建一个
|
,并且将基于
p.conditionvariable
查询
表2

(from _obj1 in table1.Where(p => p.IsActive == true))
   from prob in table2.Where(p => (p.Id == _obj1.Id && !p.IsBlocked && p.IsActive && p.ConditionVariable == 0)
                               || (p.ConditionVariable > 0 && p.Id == p.ConditionVariable && !p.IsBlocked && p.IsActive))
   select new Class1
   {
       Title = prob.Title,
       Status = prob.IsPending,
       Id = _obj1.id 
   }
如果您想使用
If/else
条件,您可以使用如下内容

(from _obj1 in table1.Where(p => p.IsActive == true))
   from prob in table2.Where(p => {
            bool state = false;
            if(p.ConditionVariable > 0)  {
                state = p.Id == p.ConditionVariable && !p.IsBlocked && p.IsActive;
            } else if(p.ConditionVariable == 0) {
                state = p.Id == _obj1.Id && !p.IsBlocked && p.IsActive;
            }
            return state;   
        })
   select new Class1
   {
       Title = prob.Title,
       Status = prob.IsPending,
       Id = _obj1.id 
   }

我将把p.ConditionVariable测试放在开头,这样它就是第一个被检查的东西(因为&操作在第一个失败的条件下停止。如果第一个检查失败,那么接下来检查| |操作):


第二个变量也可能是myvariable=0。。。。正如有人评论的那样,但在这种情况下,这并不是真正必要的,因为您那里有| |&&操作符。

我不知道您在问什么,请澄清问题。如果p.ConditionVariable!=0?如果p.ConditionVariable!=0,我需要从表2中获取id==ConditionVariable的记录。我会详细解释的。我有一个场景,其中table2有一个名为ConditionVariable和id的列。默认情况下ConditionVariable没有任何值。如果它有任何值,那么我需要在表2中搜索等于conditionvariable的id。希望你明白我的意思。我也不知道你在问什么,但听起来答案好像涉及到一个来自table2@HafizH您能给出以下解决方案的反馈吗?如果使用“p.Id==p.ConditionVariable”这个条件,它将获得Id和ConditionVariable相同的记录。但我的情况是,如果conditionvariable>0。我需要id等于该条件变量的记录。如果我把事情弄复杂了,我真的很抱歉
(from _obj1 in table1.Where(p => p.IsActive == true))
                           from prob in table2.Where(
                               (p => p.ConditionVariable == 0 && p.Id == _obj1.Id && !p.IsBlocked && p.IsActive) 
                               || (p.ConditionVariable > 0 && p.Id == p.ConditionVariable && !p.IsBlocked && p.IsActive))
                           select new Class1
                           {
                               Title = prob.Title,
                               Status = prob.IsPending,
                               Id = obj1.id 
                           }