// 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; using EF_LINQ; namespace E { public class Workaround : badparent { public IEnumerable badchild; } public class Program { public static void Main(string[] args) { // good using(EF_LINQContext ctx = new EF_LINQContext()) { foreach(goodparent gp in ctx.goodparent) { Console.Write("{0,3} {1,-10}:", gp.id, gp.fulnam); foreach(goodchild gc in gp.goodchild) { Console.Write(" {0}", gc.val); } Console.WriteLine(); } } // bad workaround using(EF_LINQContext ctx = new EF_LINQContext()) { IQueryable q = ctx.badparent.GroupJoin(ctx.badchild, bp => bp.id, bc => bc.parent_id, (bp, allbc) => new Workaround{ id = bp.id, fulnam = bp.fulnam, badchild = allbc }); foreach(Workaround w in q) { Console.Write("{0,3} {1,-10}:", w.id, w.fulnam); foreach(badchild bc in w.badchild) { Console.Write(" {0}", bc.val); } Console.WriteLine(); } } // bad two context workaround using(EF_LINQContext pctx = new EF_LINQContext()) using(EF_LINQContext cctx = new EF_LINQContext()) { IEnumerable q = pctx.badparent.ToList().GroupJoin(cctx.badchild.ToList(), bp => bp.id, bc => bc.parent_id, (bp, allbc) => new Workaround{ id = bp.id, fulnam = bp.fulnam, badchild = allbc }); foreach(Workaround w in q) { Console.Write("{0,3} {1,-10}:", w.id, w.fulnam); foreach(badchild bc in w.badchild) { Console.Write(" {0}", bc.val); } Console.WriteLine(); } } } } }