Chr*_*ård 2 c# linq ienumerable
我的代码中有以下方法链:
MyFormCollection
.Select(form => Handler.HandleForm(form))
.Select(form =>
{
form.Id = Guid.Empty;
form.OtherProperty = existingValue;
return form;
})
.ToList()
.ForEach(FormService.SaveForm);
Run Code Online (Sandbox Code Playgroud)
此代码的问题是Handler.HandleForm()在某些情况下可以返回null.如果是这种情况,我想跳过该表单的其余方法,然后继续列表中的下一个项目.
如果没有在每一步中进行空检查,有没有办法做到这一点?
我建议添加Where:
MyFormCollection
.Select(form => Handler.HandleForm(form))
.Where(form => form != null) // <- from this line on not null form(s) only
...
Run Code Online (Sandbox Code Playgroud)