public void LinqJoin() { using (var db = Helper.CreatePCORIDataContext(ConnectionString)) { var query = db.ReportedOutcomeCommonMeasures .Join(db.Encounters, pro => pro.PatientID, enc => enc.PatientID, (pro, enc) => new { PRO = pro, ENC = enc }); Logger.Debug(query.Expression.ToString()); var pe_pro = Expression.Parameter(typeof(ReportedOutcome), "pro"); var pe_enc = Expression.Parameter(typeof(Encounter), "enc"); var joinType = TypeBuilderHelper.CreateType("PRO_Enc", new IPropertyDefinition[] { new TypedPropertyDefinition <ReportedOutcome> { Name = "PRO" }, new TypedPropertyDefinition <Encounter> { Name = "ENC" } }); var joinBindings = new[] { Expression.Bind(joinType.GetProperty("PRO"), pe_pro), Expression.Bind(joinType.GetProperty("ENC"), pe_enc) }; var resultSelector = Expression.Lambda( Expression.MemberInit(Expression.New(joinType), joinBindings), pe_pro, pe_enc ); var joinCall = Expression.Call( typeof(Queryable), "Join", new Type[] { typeof(ReportedOutcome), typeof(Encounter), typeof(string), joinType }, new Expression[] { db.ReportedOutcomeCommonMeasures.AsQueryable().Expression, db.Encounters.AsQueryable().Expression, Expression.Quote(Expression.Lambda(Expression.Property(pe_pro, "PatientID"), pe_pro)), Expression.Quote(Expression.Lambda(Expression.Property(pe_enc, "PatientID"), pe_enc)), Expression.Quote(resultSelector) } ); Logger.Debug(joinCall.ToString()); } }