Is there a better lambda expression for this

I’m using a lambda expression to find the element in a list:

var ienumerable = this.deployedFirecrackers.Where(x => x.firecracker.gameObjectInstance == firecrackerGameObject);
FirecrackerListEntry fle = ienumerable.ElementAt(0);

Basically, the element is guaranteed to be in the list. And it will only exist in the list once.
Hence the code works, but is there a better/alternative lambda expression for finding the element when the above conditions are guaranteed?

Are you looking for FirstOrDefault?

Or maybe just First?

1 Like

Yes, that works out better than my original approach.

var fle = this.deployedFirecrackers.First(x => x.firecracker.gameObjectInstance == firecrackerGameObject);
1 Like

FirstOrDefault is always best since it never throw null exception on empty element.