For best performance starting settings.LabelIdsatHashSet
var labelIds = new HashSet<int>(settings.LabelIds);
Then use the hashset to quickly find O (1)
var labelAgentIds = _agents.Where(x => labelIds.Contains(x.Id)).SelectMany(x => x.AgentIds);
If you know that labelAgentIdsand settings.AgentIdsnever have the same ID, you can use Concat, or use Unionto avoid duplicates.
settings.AgentIds = new Collection<Guid>(labelAgentIds.Union(settings.AgentIds).ToList())
Use Aggregatewill be slower.
source
share