// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class A { public int AID { get; set; } public string AS { get; set; } public int BID { get; set; } public int CID { get; set; } } public class B { public int BID { get; set; } public string BS { get; set; } } public class C { public int CID { get; set; } public string CS { get; set; } } public class Program { public static void Main(string[] args) { List b = new List { new B { BID=1, BS="B1" }, new B { BID=2, BS="B2" } }; List c = new List { new C { CID=1, CS="C1" }, new C { CID=2, CS="C2" } }; List a = new List { new A { AID=1, AS="A1", BID=1, CID=2 }, new A { AID=2, AS="A2", BID=2, CID=1 } }; var q1 = a.Join(b, arow => arow.BID, brow => brow.BID, (arow,brow) => new { AS=arow.AS, CID=arow.CID, BS=brow.BS }).Join(c, abrow => abrow.CID, crow => crow.CID, (abrow,crow) => new { AS=abrow.AS, BS=abrow.BS, CS=crow.CS }); foreach (var row in q1) { Console.WriteLine("{0} {1} {2}", row.AS, row.BS, row.CS); } var q2 = from arow in a join brow in b on arow.BID equals brow.BID join crow in c on arow.CID equals crow.CID select new { AS=arow.AS, BS=brow.BS, CS=crow.CS }; foreach (var row in q2) { Console.WriteLine("{0} {1} {2}", row.AS, row.BS, row.CS); } } } }