Reputation: 69
If i have 3 tables. How can i use IEnumerable LINQ Joins to join all 3 tables together? here is my current query and I want to join another IEnumerable called BudgetTargets using the GPRow id = Budgettargets id
var rows = _context.GpRows.Join(
_context.ShopInfos,
GpRow => GpRow.ShopId,
ShopInfo => ShopInfo.ShopId,
(GpRow, ShopInfo) => new{
ShopName = ShopInfo.ShopName,
State = ShopInfo.State,
YearMonth = String.Concat(GpRow.Year, GpRow.Month),
GpRow.Year,
GpRow.Month,
GpRow.Id,
GpRow.ShopId,
GpRow.PaintSale,
GpRow.PanelSale,
GpRow.PartsSale,
GpRow.OtherSale,
GpRow.PaintCost,
GpRow.PanelCost,
GpRow.PartsCost,
GpRow.OtherCost,
GpRow.PanelWages,
GpRow.PaintWages,
GpRow.Depreciation,
GpRow.ForecastedSales,
GpRow.Expenses
}
)
.Where(GpRow => shopId_Array.Contains(GpRow.ShopId))
.OrderByDescending(a => a.Year).OrderByDescending(b => b.Month).ThenBy(c => c.State).ThenBy(c => c.ShopName)
.ToList();
Upvotes: 0
Views: 167
Reputation: 1213
I agree with @juharr's comment. The LINQ query format is sometimes clearer about intent than the fluent API style. Here is how the query would be written.
var rows = from GpRow in _context.GpRows
join ShopInfo in _context.ShopInfos on GpRow.ShopId equals ShopInfo.ShopId
orderby GpRow.ShopId descending, GpRow.Month descending, ShopInfo.State, ShopInfo.ShopName
select new {
ShopName = ShopInfo.ShopName,
State = ShopInfo.State,
YearMonth = String.Concat(GpRow.Year, GpRow.Month),
GpRow.Year,
GpRow.Month,
GpRow.Id,
GpRow.ShopId,
GpRow.PaintSale,
GpRow.PanelSale,
GpRow.PartsSale,
GpRow.OtherSale,
GpRow.PaintCost,
GpRow.PanelCost,
GpRow.PartsCost,
GpRow.OtherCost,
GpRow.PanelWages,
GpRow.PaintWages,
GpRow.Depreciation,
GpRow.ForecastedSales,
GpRow.Expenses
};
Upvotes: 1