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;
}
精彩评论