Search code examples
linqt-sqlcross-join

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 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
**erm.....**
)

Brain in meltdown, any help appreciated...


Solution

  • 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() .....