我不知道如何将这个简单的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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想要的只是一个布尔结果,你不需要where或select.使用 Any方法:
bool createAccount = !transactionDetails.Any(
td => td.RequestType == ... && td.ResponseType == ...);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
537 次 |
| 最近记录: |