[SOLVED] Can I use a Lamdba here?

I have this code block in my script with an if/then/else but I want to know if it can also be done with a Lambda instead and if so, how I can implement it.

void DoThis(){
var enemies = FindComponentsOfType(typeof(Stats))..gameObject.where x != gameObject;
}

Not sure what FindComponentsofType is doing but if you just mean the where clause then

FindComponentsOfType(typeof(Stats)).Where(x => x.gameObject != gameObject);

FindComponents returns an array of components of the type specified. I only want it to return those that are not the game object itself.

I more meant - “This should work assuming your method is returning an IEnumerable”

Object does not implement IEnumerable so it’s not working. :frowning:

I don’t think that’s the right way to handle that. If you’re going to be operating on IEnumerable anyway, use one to start with. In general, though, you posted too much that was irrelevant to the question, or unknown to us. Please try to provide more clarity in the future.

void DoThis() {    // Why is this relevant to your question?
    var enemies // This name doesn't make sense. You're getting Stats. Stats are not enemies.
        = Components // What goes here? Is it even relevant to your question?
        .OfType<Stats>()
        .Where(stats => stats.gameObject != gameObject);
}