k/SceneExtensions.cs
using System.Collections.Generic;
using System.Linq;

namespace Sandbox.k;

public static class SceneExtensions
{
	[Pure]
	public static IEnumerable<(T First, K Second)> Filter<T, K>(this Scene scene) where T : Component where K : Component
	{
		// var firstSet = scene.GetAllComponents<T>().ToList();
		var firstSet = scene.GetAllComponents<T>();
		if (!firstSet.Any())
			yield break;
		// var secondSet = scene.GetAllComponents<K>().ToList();
		var secondSet = scene.GetAllComponents<K>();
		if (!secondSet.Any())
			yield break;

		foreach (var first in firstSet)
		{
			if (first is null) continue;
			if (first is IValid v1 && !v1.IsValid) continue;

			var entityId = first.GameObject.GetHashCode();

			foreach (var second in secondSet)
			{
				if (second == null) continue;
				if (second.GameObject.GetHashCode() != entityId) continue;
				if (second is IValid v2 && !v2.IsValid) continue;

				yield return (first, second);
				break;
			}
		}
	}
}