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

如何在Microsoft RulesEngine自定义错误消息中拼接匹配集合项属性值

解决方案

第一步:调整规则配置

在原有规则基础上新增ErrorMessageParameters节点,用于定义每个占位符对应的取值表达式,规则触发后引擎会自动计算表达式结果并替换占位符。

{
    "RuleName": "CheckforVTPumpSelectionErrors121-03",
    "ErrorMessage": "WARNING: 121 - VT PUMP Manual Pick Required for {0} HP {1} pump!",
    "Expression": "ProductFamily.PumpConfigurations.Any(r => r.PumpDescription == \"Water Feature\" || r.PumpDescription == \"Transfer\") && Flag1 == false",
    "ErrorMessageParameters": [
        "ProductFamily.PumpConfigurations.First(r => r.PumpDescription == \"Water Feature\" || r.PumpDescription == \"Transfer\").HP",
        "ProductFamily.PumpConfigurations.First(r => r.PumpDescription == \"Water Feature\" || r.PumpDescription == \"Transfer\").PumpDescription"
    ]
}

上述示例默认取第一个匹配的泵配置填充占位符,如果你需要拼接所有匹配项的属性值,可将参数表达式调整为集合拼接逻辑,比如所有符合要求的HP值拼接可以写为:string.Join(',', ProductFamily.PumpConfigurations.Where(r => r.PumpDescription == \"Water Feature\" || r.PumpDescription == \"Transfer\").Select(r => r.HP))

第二步:C#侧调用示例

// 初始化规则引擎,传入你解析后的规则配置
var rulesEngine = new RulesEngine.RulesEngine(yourRuleConfigList);

// 构造业务输入参数
var inputParams = new dynamic[] {
    new {
        ProductFamily = yourProductFamilyDtoInstance,
        Flag1 = false // 替换为实际业务中Flag1的取值
    }
};

// 执行规则校验
var ruleResults = await rulesEngine.ExecuteAllRulesAsync("YourWorkflowName", inputParams);

// 读取已自动填充占位符的错误消息
foreach (var result in ruleResults)
{
    if (!result.IsSuccess)
    {
        Console.WriteLine(result.ErrorMessage);
    }
}

注意事项

  • 你定义的PumpConfiguration类中HP、PumpDescription的set访问符是internal,如果规则引擎调用代码和DTO定义不在同一个程序集,需要将访问符改为public,否则表达式会无法读取属性值。
  • 如果存在多个匹配的泵配置项,可根据业务需求调整ErrorMessageParameters中的表达式,避免只取第一条导致信息遗漏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 07:12:04