Trying to convert cross join transact-sql into LINQ
I'm trying to get my head around this cross join (something that I've never seen before) so that I can convert into LINQ. I'm not exactly sure what its trying to do:
Select various a,b,c,d, and e columns
From Fee a
inner Join FeeCategory b on a.CategoryID = b.FeeCategoryID
inner join FeeCompanyType c on a.FeeID = c.FeeID
cross join FeeType e
left outer join FeeTypeLink d on a.FeeID = d.FeeID and e.FeeTypeID = d.FeeTypeID
to
var q = (
from fees in Ses开发者_开发百科sion.Query<Fee>()
join feeCategories in Session.Query<FeeCategory>() on fees.FeeCategory.Id equals feeCategories.Id
join feeCompanyType in Session.Query<FeeCompanyType>() on fees.Id equals feeCompanyType.Fee.Id
**erm.....**
)
Brain in meltdown, any help appreciated...
For cross join just add another from
:
from fees in Session.Query<Fee>()
//cross join:
from feetypes in Session.Query<Session.Query<Fee>()
join ...
join ...
// left join:
join feetypelink in Session.Query<FeeTypeLink>() on
new {fees.FeeID,feetypes.FeeTypeID} equals new{feetypelink.FeeID,feetypelink.FeeTypeID}
into gr
from res in gr.DefaultIfEmpty() .....
Just throw another from
in there, since you're not truly "joining" the tables, from a LINQ perspective.
from fees in Session.Query<Fee>()
join feeCategories in Session.Query<FeeCategory>() on fees.FeeCategory.Id equals feeCategories.Id
join feeCompanyType in Session.Query<FeeCompanyType>() on fees.Id equals feeCompanyType.Fee.Id
from feeType in Session.Query<FeeType>()
select ...
The original query is nonsense and not really doing a cross join
In fact it is equivalent to this
Select various a,b,c,d, and e columns
From Fee a
inner Join FeeCategory b on a.CategoryID = b.FeeCategoryID
inner join FeeCompanyType c on a.FeeID = c.FeeID
left join FeeTypeLink d on a.FeeID = d.FeeID
left join Feetype e ON e.FeeTypeID = d.FeeTypeID
Now you should be able to convert it easily to Linq to SQL. I don't use that much so will leave others to fill in the details. You don't need anything fancy though.
You can use this:
from a in ctx.As
join b in ctx.Bs on a.ID equals b.IdOfA //inner join for entities without relationships
join c in ctx.Cs on 1 equals 1 //inner join with constant condition
精彩评论