如何将foreach转换为Linq

Byr*_*ock 0 linq

我不知道如何将这个简单的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)

Mat*_*nen 6

如果你想要的只是一个布尔结果,你不需要whereselect.使用 Any方法:

bool createAccount = !transactionDetails.Any(
    td => td.RequestType == ... && td.ResponseType == ...);
Run Code Online (Sandbox Code Playgroud)