# How to trigger poison effects efficiently

I'm trying to trigger poison effects at the end of each turn in a turn based game. Each Actor in battle has a list of active effects, each of which could be a poison. I simply want to Execute() all of those Poisons.

See my code below; is there a way I can combine my two queries or make them more efficient so that the moment a Poison is found, that Poison executes?

var poisonedActors =
from Actor actor in ins.BattleParticipants
where actor.ActiveEffects.Any(activeEffect => activeEffect is Poison)
select actor;
var poisons =
from ActiveEffect activeEffect in poisonedActors
where activeEffect is Poison
select activeEffect;
foreach (Poison poison in poisons)
poison.Execute();


EDIT: Is this more efficient? Is there a better way to do things?

    foreach (Actor actor in ins.BattleParticipants)
foreach (ActiveEffect activeEffect in actor.ActiveEffects)
if (activeEffect is Poison)
activeEffect.Execute();

• Does a poison effect disappear once it's triggered? Is it just poison effects that are "triggered"? Aug 21 '17 at 23:14
• No. I just want to trigger all poison effects at the same time. Other effects can be triggered as well, but that's not the focus right now. Aug 21 '17 at 23:16
• The select statements are from LINQ (Language Integrated Query), a very readable database language integrated into C#. They are indeed code! Aug 22 '17 at 3:13
• How many entities are there going to be? This might be premature optimization. If you just want to know which is faster for curiosity's sake, I would suggest cranking the number of entities up really high and timing it, or examining the generated CLR code. It's possible the compiler crunches those examples to the same thing in the end! Aug 22 '17 at 5:31
• In the first example you check the same activeeffects twice, in the second only once. So it looks like the second could be more efficient, but that is not much more than a guess without some testing. Aug 22 '17 at 8:40

A "performance critical" part of your code is the check if (activeEffect is Poison)
It looks as though Poison inherits from ActiveEffect but that it is different from the other classes that inherit from ActiveEffect since you want to call execute separately.
I would do this if performance was critical: create a separate list (ActiveEffectsExecuteOnTurnEnd) in 'Actor' where you only put stuff that will execute at the end of the turn.