使用多个 IEnumerable 连接
using multiple IEnumerable Joins
如果我有3张桌子。我如何使用 IEnumerable LINQ 连接将所有 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
};
如果我有3张桌子。我如何使用 IEnumerable LINQ 连接将所有 3 个表连接在一起?这是我当前的查询,我想使用 GPRow id = Budgettargets id
加入另一个名为 BudgetTargets 的 IEnumerablevar 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
};