using System.Collections.Generic;
public static void Main()
var products = new List<Product>{
new Product{Id=44,Name="Akkusav 1L"},
new Product{Id=303,Name="AKKU YTX4L-BS LP jobb P 114x71x86"},
var compatibilities = new List<Compatible>{
new Compatible{Id=1,Prod1_id=44,Prod2_id=303},
List<long> addedProducts = new List<long>();
var p1_comps = GetAllCompatibles(compatibilities,44,44,addedProducts);
addedProducts = new List<long>();
var p2_comps = GetAllCompatibles(compatibilities,303,303,addedProducts);
static List<long> GetCompatibles(IEnumerable<Compatible> table, long productId, long originalId, List<long> addedProducts)
(c.Prod1_id == productId &&
c.Prod2_id != originalId &&
!addedProducts.Contains(c.Prod2_id))
).Select(c => c.Prod2_id).ToList();
static List<long> GetAllCompatibles(List<Compatible> table,long pid, long origid, List<long> addedProducts)
List<long> result = new List<long>();
var Childs = GetCompatibles(table, pid, origid, addedProducts);
Childs.AddRange(GetCompatibles(table.Select(t =>
new Compatible { Prod1_id = t.Prod2_id, Prod2_id = t.Prod1_id }
).ToList(), pid, origid, addedProducts));
addedProducts.AddRange(Childs);
foreach (long child in Childs)
var temp = GetAllCompatibles(table, child, origid, addedProducts);
result.AddRange(temp.Select(p =>
GetCompatibles(table, p, origid, addedProducts)
{ l1.AddRange(l2); return l1; })
return result.Distinct().ToList();