问题描述
如果我有3张桌子。如何使用IEnumerable LINQ Joins将所有3个表连接在一起?这是我当前的查询,我想使用GPRow id = Budgettargets id加入另一个名为BudgetTargets的IEnumerable
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();
解决方法
我同意@juharr的评论。 LINQ查询格式的目的有时比流利的API风格更清楚。这是查询的编写方式。
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
};