LINQ multiple join IQueryable modify result selector expression
You can define a throwaway intermediary object to select into instead of using an anonymous type:
public class JoinedItem
{
public TableA TableA { get; set; }
public TableB TableB { get; set; }
public TableC TableC { get; set; }
}
New method:
public IQueryable<T> GetJoinedView<T>(Expression<Func<JoinedItem, T>> selector)
{
return DbContext.Set<TableA>()
.Join(DbContext.Set<TableB>(),
a => a.ID,
b => b.TableAID,
(a, b) => new { A = a, B = b})
.Join(DbContext.Set<TableC>(),
ab => ab.B.ID,
c => c.TableBID
(ab, c) => new JoinedItem
{
TableA = ab.A,
TableB = ab.B,
TableC = c
})
.Select(selector);
}
Will you really be joining on these three tables enough to make the use of this method clearer than just expressing what you want to do directly in LINQ? I would argue that the extra lines needed to create this query every time would be clearer than using this method.