Zai*_*ain 2 c# linq each for-loop
我对linq online的解释感到有些困惑,所以我想我会创建一个帖子.
我想用linq语句替换我的foreach语句,因为我确信做这样的小事会让我的程序更好.
我想用LINQ做的例子:
public bool CheckForAccount(int accountID)
{
bool status = false;
foreach (AccountHolders accountHolder in AccountHoldersList)
{
if (accountHolder.AccountNumber == accountID)
{
status = true;
break;
}
}
return status;
}
Run Code Online (Sandbox Code Playgroud)
请你能解释它是如何工作的,所以我理解你在做什么.
提前致谢!
所以可能最简洁的陈述是:
public bool CheckForAccount(int accountId)
{
return this.AccountHolderList.Any(x => x.AccountNumber == accountId);
}
Run Code Online (Sandbox Code Playgroud)
用英语说
检查
AccountHolderList对任何情况下的AccountNumber财产等于accountId.如果有,则返回true,否则返回false.