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

如何通过Linq/Lambda筛选符合条件的BState列表而非bool列表

修正C#筛选BState列表的代码

你之前用Select+Any得到的是布尔值列表,因为Select是把每个元素转换成另一种类型(这里是bool)。要拿到符合条件的BState实例,直接用Where替换Select即可:

var filteredBStates = response.CurBStates
    .Where(c => 
        // 先过滤TerminalId为0的BState
        c.CurBState.TerminalId == 0 &&
        // 再判断是否存在满足条件的UResource
        response.UResources.Any(u => 
            u.Type == RType.B && 
            u.UReason != UReason.BCM && 
            u.Name != c.BId.ToString()
        )
    )
    .ToList();

逻辑说明

  • Where方法会遍历CurBStates中的每个元素,只保留满足括号内条件的BState
  • 条件分两层:
    1. 先筛选自身TerminalId == 0的BState
    2. 再检查是否存在对应的UResource,同时满足类型为RType.B、原因不是UReason.BCM、名称不等于当前BState的BId字符串形式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:53:09