我不知道如何将这个简单的foreach转换为linq。
看起来很简单,但我一直收到编译错误?
var createAccount = from TransactionDetail td in transactionDetails
Where
td.ResponseType == ResponseType.SUCCESS AND
td.RequestType == RequestType.CREATE_ACCOUNT
SELECT true;
/* trying to convert this */
bool createAccount = true;
foreach(TransactionDetail td in transactionDetails )
{
if (td.RequestType == RequestType.CREATE_ACCOUNT)
{
if (td.ResponseType == ResponseType.SUCCESS)
{
createAccount = false;
}
}
}
答案 0 :(得分:6)
如果你想要的只是一个布尔结果,你不需要where
或select
。使用Any
方法:
bool createAccount = !transactionDetails.Any(
td => td.RequestType == ... && td.ResponseType == ...);