How to create a dynamic Linq Join extension method?

I've fixed it myself now. It was a schoolboy error passing too many parameters to the CreateQuery(... ) call. Paste the following code into the Dynamic.Cs file within the DynamicQueryable class for a dynamic Join extension method.

You can find the source for the DynamicQuery sample project at code.msdn.microsoft.com/csharpsamples. Enjoy. Public static IQueryable Join(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object values) { if (inner == null) throw new ArgumentNullException("inner"); if (outerSelector == null) throw new ArgumentNullException("outerSelector"); if (innerSelector == null) throw new ArgumentNullException("innerSelector"); if (resultsSelector == null) throw new ArgumentNullException("resultsSelctor"); LambdaExpression outerSelectorLambda = DynamicExpression.

ParseLambda(outer. ElementType, null, outerSelector, values); LambdaExpression innerSelectorLambda = DynamicExpression. ParseLambda(inner.AsQueryable().

ElementType, null, innerSelector, values); ParameterExpression parameters = new ParameterExpression { Expression. Parameter(outer. ElementType, "outer"), Expression.

Parameter(inner.AsQueryable(). ElementType, "inner") }; LambdaExpression resultsSelectorLambda = DynamicExpression. ParseLambda(parameters, null, resultsSelector, values); return outer.Provider.

CreateQuery( Expression. Call( typeof(Queryable), "Join", new Type {outer. ElementType, inner.AsQueryable().

ElementType, outerSelectorLambda.Body. Type, resultsSelectorLambda.Body. Type }, outer.

Expression, inner.AsQueryable(). Expression, Expression. Quote(outerSelectorLambda), Expression.

Quote(innerSelectorLambda), Expression. Quote(resultsSelectorLambda))); } //The generic overload. Public static IQueryable Join(this IQueryable outer, IEnumerable inner, string outerSelector, string innerSelector, string resultsSelector, params object values) { return (IQueryable)Join((IQueryable)outer, (IEnumerable)inner, outerSelector, innerSelector, resultsSelector, values); }.

Example of usage is here: stackoverflow. Com/questions/5996403/… – alpav May 18 at 19:01.

Thanks for this, great stuff. After much mucking around with the selector I found while I was debugging it you can use the "inner" and "outer" keywords.

Here is some sample code showing a join on multiple columns. Using a datatable and datarows you need to always access fields via the indexer. DataTable t1 = new DataTable(); t1.Columns.

Add("FundId", typeof(int)); t1.Columns. Add("Date", typeof(DateTime)); t1.Columns. Add("CodeA", typeof(string)); t1.Rows.

Add(1, new DateTime(2010, 01, 01), "A1"); t1.Rows. Add(2, new DateTime(2010, 01, 01), "A2"); t1.Rows. Add(3, new DateTime(2010, 01, 01), "A3"); DataTable t2 = new DataTable(); t2.Columns.

Add("FundId", typeof(int)); t2.Columns. Add("Date", typeof(DateTime)); t2.Columns. Add("CodeB", typeof(string)); t2.Rows.

Add(1, new DateTime(2010, 01, 01), "B1"); t2.Rows. Add(2, new DateTime(2010, 01, 01), "B2"); t2.Rows. Add(3, new DateTime(2010, 01, 01), "B3"); IQueryable outerTable = t1.AsEnumerable().AsQueryable(); IEnumerable innerTable = t2.AsEnumerable(); var query = outerTable.

Join ( innerTable, "new(get_Item(0) as FundId, get_Item(1) as Date)", "new(get_Item(0) as FundId, get_Item(1) as Date)", "new(outer. Get_Item(0) as FundId, outer. Get_Item(2) as CodeA, inner.

Get_Item(2) as CodeB)" ).

LambdaExpression outerSelectorLambda = DynamicExpression. LambdaExpression innerSelectorLambda = DynamicExpression. ElementType, "outer"), Expression.

LambdaExpression resultsSelectorLambda = DynamicExpression. New Type {outer. //The generic overload.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions