将if条件转换为linq [英] Convert an if condition to linq

查看:77
本文介绍了将if条件转换为linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此if条件转换为LINQ语句,或使用三元条件



  if (MenuEnabled ==  false 
{
_parentForm.CheckMenu();
}
else if (MenuEnabled == true
{
_parentForm.CheckMenu( true );
}





我的尝试:



没有成功按预期工作

解决方案

您不需要LINQ或三元运算符。您只需要

 _ parentForm.CheckMenu(MenuEnabled); 







以上评论是真实的,并回答了你的问题,但我担心你写这些陈述的方式 - 现在也可以在你养成坏习惯之前先了解这一点:)



您不需要使用 == false == true 使用布尔值时。只需使用(for == true)

  if (MenuEnabled)

和(for == false)

  if (!MenuEnabled)





还有你不需要 if ... else with bools - 它们只能是true或false所以不需要第二个如果


How can I convert this if condition to a LINQ statment, or using a ternary condition

if (MenuEnabled == false)
   {
       _parentForm.CheckMenu(false);
   }
else if (MenuEnabled == true)
   { 
       _parentForm.CheckMenu(true);
   }



What I have tried:

nothing successful that worked as intended

解决方案

You don't need LINQ or a ternary operator. All you need is

_parentForm.CheckMenu(MenuEnabled);



[Edit]
The above comment is true and answers your question but I am concerned about the way you have written these statements - may as well learn this now before you get into bad habits :)

You don't need to use == false or == true when using Booleans. Just use (for == true)

if (MenuEnabled)

and (for == false)

if (!MenuEnabled)



Also you don't need the if ... else if with bools - they can only be true or false so there is no need for the second if


这篇关于将if条件转换为linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆