diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs index a771f89878..41cceb25a3 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs @@ -61,26 +61,23 @@ public static class WorkflowBuilderExtensions Throw.IfNull(targets); Func predicate = WorkflowBuilder.CreateConditionFunc(IsAllowedTypeAndMatchingCondition)!; - List targetList = new(); - foreach (ExecutorBinding target in targets) - { - Throw.IfNull(target, nameof(targets)); - targetList.Add(target); +#if NET6_0_OR_GREATER + if (targets.TryGetNonEnumeratedCount(out int count) && count == 1) +#else + if (targets is ICollection { Count: 1 }) +#endif + { + return builder.AddEdge(source, Throw.IfNull(targets.First(), nameof(targets)), predicate); } - Throw.IfNullOrEmpty(targetList, nameof(targets)); - - if (targetList.Count == 1) - { - return builder.AddEdge(source, targetList[0], predicate); - } - - return builder.AddSwitch(source, (switch_) => switch_.AddCase(predicate, targetList)); + return builder.AddSwitch(source, (switch_) => switch_.AddCase(predicate, targets.Select(ValidateTarget))); // The reason we can check for "not null" here is that CreateConditionFunc will do the correct unwrapping // logic for PortableValues. bool IsAllowedTypeAndMatchingCondition(TMessage? message) => message != null && (condition == null || condition(message)); + + ExecutorBinding ValidateTarget(ExecutorBinding target) => Throw.IfNull(target, nameof(targets)); } ///