开发者

how to write LINQ to objects equivalent code for this code

开发者 https://www.devze.com 2023-04-12 23:31 出处:网络
How can we write LINQ equivalent code of this code: foreach (Function objFunc in _objCFLFile.CFLFunctionsList)

How can we write LINQ equivalent code of this code:

foreach (Function objFunc in _objCFLFile.CFLFunctionsList)
{
     if (objFunc.Name == txtFunctionName.Text)
     {
          ShowMessage(Constants.M10036);
          return false;
     }
}

thanks guys i marked your answers 开发者_如何学Pythonas +1 If i wish to add one more condition like this

foreach (Function objFunc in _objCFLFile.CFLFunctionsList)
            {
                if (objFunc.Name == txtFunctionName.Text && objFunc.Signature == OtherFunction.Signature)
                {
                    ShowMessage(Constants.M10046);//function already exists
                    return false;
                }
            }

in this case also there are various LINQ syntax but which will b the perfect one , efficient one ?


if (_objCFLFile.CFLFunctionsList.Any(f => f.Name == txtFunctionName.Text))
{
  ShowMessage(Constants.M10036);
  return false;
}

With your extra requirement:

if (_objCFLFile.CFLFunctionsList.Any(f => f.Name == txtFunctionName.Text &&
                                     f.Signature == OtherFunction.Signature))
{
  ShowMessage(Constants.M10036);
  return false;
}

If you have many more conditions to add it might be worth creating a method to do the comparison.


Using Any():

if (_objCFLFile.CFLFunctionsList.Any(objFunc => 
                  objFunc.Name == txtFunctionName.Text))            
            {
                ShowMessage(Constants.M10036);                     
                return false; 
            } 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号