Imagine the following table structure
---------
TableA
ID
Name
---------
TableB
ID
TableAID
---------
TableC
ID
TableBID
I want to define a function that joins these three tables and accepts an Expression<Func<TableA, TableB, TableC, T>>
as a selector.
So I'd like something like the following:
public IQueryable<T> GetJoinedView<T>(Expression<Func<TableA, TableB, TableC, T>> selector)
{
return from a in DbContext.Set<TableA>()
join b on DbContext.Set<TableB>() a.ID equals b.TableAID
join c on DbContext.Set<TableC>() b.ID equals c.TableBID
select selector;
}
Now, obviously the above doesn't do what I want it to do, this will give me an IQueryable
of the expression type. I could use method chaining syntax, but then I end up needing multiple selectors, one for each method chain invocation. Is there a way to take the selector and apply it to an anonymous type like in the following incomplete function:
public IQueryable<T> GetJoinedView<T>(Expression<Func<TableA, TableB, TableC, T>> selector)
{
var query = from a in DbContext.Set<TableA>()
join b on DbContext.Set<TableB>() a.ID equals b.TableAID
join c on DbContext.Set<TableC>() b.ID equals c.TableBID
select new
{
A = a, B = b, C = c
};
// I need the input selector to be modified to be able to operate on
// the above anonymous type
var resultSelector = ModifyInputSelectorToOperatorOnAnonymousType(selector);
return query.Select(resultSelector);
}
Any ideas on how this could be done?