Adding a third left join drops initial table out of scope
If I do a LINQ query with 2 left joins, I'm fine, but once I add a third
left join my initial table in my "from" is no longer available to the 3rd
left join and I get an error:
Source compile error, line ##: The name 'x' does not exist in the current
context
For example:
from x in [TableX]
join a in [TableA]
on x.ItemNumber equals a.ItemNumber into aLeftJoin
from aLeft in aLeftJoin.DefaultIfEmpty()
join b in [TableB]
on x.ItemNumber equals b.ItemNumber into bLeftJoin
from bLeft in bLeftJoin.DefaultIfEmpty()
join c in [TableC]
on x.ItemNumber equals c.ItemNumber into cLeftJoin
from cLeft in cLeftJoin.DefaultIfEmpty()
select new
{
x.ItemNumber,
...
}
Then I get the error on line on x.ItemNumber equals c.ItemNumber into
cLeftJoin and it doesn't matter if I reorder the left joins, it still
errors on the third one. This of course is a simplified example of my
actual query. Here's the full one:
from iu in [ItemUnits]
join iuPricing in (from x in [ItemUnits] where x.UnitIsPricing == "Y"
group x by new { x.ItemNumber } into grp select new { ItemNumber =
grp.Key.ItemNumber, UnitCode = grp.Min(a => a.UnitCode) })
on iu.ItemNumber equals iuPricing.ItemNumber into iuPricingLeftJoin
from iuPr in iuPricingLeftJoin.DefaultIfEmpty()
join iuStocking in (from x in [ItemUnits] where x.UnitIsStocking == "Y"
group x by new { x.ItemNumber } into grp select new { ItemNumber =
grp.Key.ItemNumber, UnitCode = grp.Min(a => a.UnitCode) })
on iu.ItemNumber equals iuStocking.ItemNumber into iuStockingLeftJoin
from iuSt in iuStockingLeftJoin.DefaultIfEmpty()
join iuPOS in (from x in [ItemUnits] where x.UnitIsPOS == "Y" group x by
new { x.ItemNumber } into grp select new { ItemNumber =
grp.Key.ItemNumber, UnitCode = grp.Min(a => a.UnitCode) })
on iu.ItemNumber equals iuPOS.ItemNumber into iuPOSLeftJoin from iuPO
in iuPOSLeftJoin.DefaultIfEmpty()
join iuPuchasing in (from x in [ItemUnits] where x.UnitIsPuchasing == "Y"
group x by new { x.ItemNumber } into grp select new { ItemNumber =
grp.Key.ItemNumber, UnitCode = grp.Min(a => a.UnitCode) })
on iu.ItemNumber equals iuPuchasing.ItemNumber into
iuPuchasingLeftJoin from iuPur in iuPuchasingLeftJoin.DefaultIfEmpty()
join iuMinNumber in (from x in [ItemUnits] group x by new { x.ItemNumber }
into grp select new { ItemNumber = grp.Key.ItemNumber, UnitCode =
grp.Min(a => a.UnitCode) })
on iu.ItemNumber equals iuMinNumber.ItemNumber
join i in [Items]
on iu.ItemNumber equals i.ItemNumber
where iu.ItemNumber.Trim() != ""
&& iu.UnitCode.Trim() != ""
orderby iu.ItemNumber,iu.UnitCode
select new
{
x.ItemNumber,
...
}
A lot going on, I know. But I don't see why it would error on the 3rd left
join.
No comments:
Post a Comment