c# - 如何将值列表插入到 Enumerable 中?
问题描述
我必须查看 IEnumerable 中的第一个元素是否等于某个值,如果是,我必须将其更新为新列表
我正在尝试使用类似的东西
if (sequence.First().Equals(value))
{
return newValues.Prepend(sequence.Skip(1));
}
因为我必须是最通用的,但是当我尝试测试这个调用时它只返回 null
解决方案
你不能修改IEnumerable
. 但是你可以使用这种方法:
// your sample:
IEnumerable<int> sequence = new[] {2, 5, 1};
int value = 2;
IEnumerable<int> newValues = new[] {1, 2, 3};
if (sequence.First() == value)
return newValues.Concat(sequence.Skip(1)).ToList()
else
return sequence.ToList();
推荐阅读
- firebase - 有限的firebase查询功能是否有解决方法
- python - Pandas DataFrame 构造函数对行进行排序,即使使用 OrderedDict 作为输入
- .net - 您应该如何分配 azure 函数来接收不同的标注或 webhook?
- ios - 某些视图上的导航栏大标题问题
- javascript - Firebase 将 Sapper 应用程序部署为云功能失败
- python - AWS lambda python多文件应用程序无法从另一个导入
- microsoft-graph-api - 下一页事件列表上的 MS 图 InvalidDeltaToken
- python - Pandas - 合并过滤最后一个时间戳的几乎重复的行
- java - 如何将字符串从控制器传递到表单的 post 方法作为参数?
- swift - 尝试用不同 .scn 文件中的节点替换ChildNode,为什么它不起作用?