You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在foreach遍历到指定值后更新列表中后续记录?

实现方案与LINQ适用性分析

LINQ完全适用,结合简单的遍历或LINQ操作就能实现需求,核心是定位到IsNext=true的记录后,修改其后续所有记录的状态。


后端数据处理(C#示例)

假设你的工序列表是List<ProcessStepModel> processSteps,按以下逻辑处理:

// 找到第一个IsNext为true的记录索引(确保列表按工序顺序排列)
var targetIndex = processSteps.FindIndex(step => step.IsNext);

// 若找到目标记录,将其之后所有记录的ReachedCurrentPosition设为false
if (targetIndex != -1 && targetIndex < processSteps.Count - 1)
{
    // 方式1:直接遍历修改
    for (int i = targetIndex + 1; i < processSteps.Count; i++)
    {
        processSteps[i].ReachedCurrentPosition = false;
    }

    // 方式2:用LINQ Skip筛选后修改
    processSteps.Skip(targetIndex + 1)
                .ToList()
                .ForEach(step => step.ReachedCurrentPosition = false);
}

注意:因为ProcessStepModel是引用类型,Skip返回的序列元素指向原列表对象,所以ToList().ForEach能直接修改原列表的状态。


前端Razor代码翻译(中文注释版)

@foreach (var step in ProcessSteps)
{
    <GridColumns>
        <GridColumn Field=@nameof(step.IsNext) HeaderText="设置下一步" TextAlign="TextAlign.Left" Width="110">
            <Template>
                @{
                    var currentStep = context as ProcessStepModel;
                }

                @if (currentStep.IsNext)
                {
                    <i>当前位置</i>
                    currentStep.ReachedCurrentPosition = true;
                }

                @if (currentStep.IsTransacted == 'Y' && currentStep.ReachedCurrentPosition != true)
                {
                    <SfButton OnClick="@(args => OpenDialog(currentStep.Id))" IsToggle="true" IsPrimary="true">将PCB移至该工序</SfButton>
                }
            </Template>
        </GridColumn>
        <GridColumn Field=@nameof(step.ProcessName) HeaderText="工序名称" Width="110"></GridColumn>
        <GridColumn Field=@nameof(step.BenchName) HeaderText="工位名称" Width="110"></GridColumn>
        <GridColumn Field=@nameof(step.IsTransacted) HeaderText="已处理" Width="110"></GridColumn>
    </GridColumns>
}

补充说明

  1. 必须保证ProcessSteps列表严格按工序顺序排列,否则定位到的IsNext=true记录位置错误,会导致后续修改逻辑失效。
  2. 前端模板中直接修改currentStep.ReachedCurrentPosition的写法虽能生效,但更推荐在后端提前处理好状态,前端仅负责渲染,避免前端状态不一致的问题。

内容的提问来源于stack exchange,提问作者sinfella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:50:24