Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将if和foreach语句转换为select和linq中的where_Linq_Asp.net Core_Stripe Payments - Fatal编程技术网

将if和foreach语句转换为select和linq中的where

将if和foreach语句转换为select和linq中的where,linq,asp.net-core,stripe-payments,Linq,Asp.net Core,Stripe Payments,如何使用选择和where将我的if语句和foreach更改为linq中更干净的语句 我尝试将if语句转换为where子句,然后使用select查询替换Foreach循环,但该循环似乎存在类型问题,无法工作 { StripeConfiguration.ApiKey = _appSettings.StripeSecretKey; var profile = await _userManager.FindByIdAsync(custom

如何使用选择where将我的if语句和foreach更改为linq中更干净的语句

我尝试将if语句转换为where子句,然后使用select查询替换Foreach循环,但该循环似乎存在类型问题,无法工作

{
                StripeConfiguration.ApiKey = _appSettings.StripeSecretKey;

                var profile = await _userManager.FindByIdAsync(customerServiceID);
                var stripeId = profile.StripeAccountId;
                if (stripeId == null)
                    throw new ArgumentException("No associated Stripe account found.");

                List<PaymentMethodDto> result = new List<PaymentMethodDto>();

                var options = new PaymentMethodListOptions
                {
                    Customer = stripeId,
                    Type = "card",
                };

                var service = new PaymentMethodService();

                var payments = await service.ListAsync(options);

                if (payments != null && payments.Data?.Count > 0)
                {
                    payments.Data.ForEach((x) =>
                    {
                        result.Add(
                            new PaymentMethodDto
                            {
                                Brand = x.Card.Brand,
                                LastDigits = x.Card.Last4,
                                StripeToken = x.Id,
                                CustomerID = x.CustomerId
                            });
                    });
                }
                return result;
            }
{
StripeConfiguration.ApiKey=\u appSettings.StripeSecretKey;
var profile=await\u userManager.FindByIdAsync(customerServiceID);
var stripeId=profile.StripeAccountId;
if(stripeId==null)
抛出新ArgumentException(“未找到关联的条带帐户”);
列表结果=新列表();
var options=新付款方法列表选项
{
客户=stripeId,
Type=“卡”,
};
var service=new PaymentMethodService();
var payments=wait service.ListAsync(选项);
如果(付款!=null&&payments.Data?.Count>0)
{
支付.数据.ForEach((x)=>
{
结果。添加(
新的付款方式
{
品牌=x.Card.Brand,
LastDigits=x.Card.Last4,
StripeToken=x.Id,
CustomerID=x.CustomerID
});
});
}
返回结果;
}

只要做一个常规的
选择即可

List<PaymentMethodDto> result = payments.Data.Select(x => new PaymentMethodDto
                            {
                                Brand = x.Card.Brand,
                                LastDigits = x.Card.Last4,
                                StripeToken = x.Id,
                                CustomerID = x.CustomerId
                            })
                            .ToList();
List result=payments.Data。选择(x=>newpaymentmethoddto
{
品牌=x.Card.Brand,
LastDigits=x.Card.Last4,
StripeToken=x.Id,
CustomerID=x.CustomerID
})
.ToList();
如果
payments.Data
中没有任何内容,这将为您提供一个空列表,这是您想要的


如果
payments
null
,您将得到一个异常,我认为如果您认真考虑,这可能也是您在这种情况下真正想要的。为什么
.ListAsync()
会产生一个空值?

什么时候
付款
会是
空的