Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 278469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:14:37+00:00 2026-05-12T01:14:37+00:00

I asked a question earlier about why left joins in Linq can’t use defined

  • 0

I asked a question earlier about why left joins in Linq can’t use defined relationships; to date I haven’t got a satisfactory response.

Now, on a parallel track, I’ve accepted that I need to use the join keyword as if there were no relationship defined between my objects, and I’m trying to work out how to express my query in Linq. Trouble is, it’s a conglomeration of left joins between multiple tables, with multiple fields involved in the join. There’s no way of simplifying this, so here’s the SQL in all its unmasked glory:

select *
from TreatmentPlan tp
join TreatmentPlanDetail tpd on tpd.TreatmentPlanID = tp.ID
join TreatmentAuthorization auth on auth.TreatmentPlanDetailID = tpd.ID
left join PatientServicePrescription rx on tpd.ServiceTypeID = rx.ServiceTypeID
left join PayerServiceTypeRules pstr on auth.PayerID = pstr.PayerID and tpd.ServiceTypeID = pstr.ServiceTypeID and pstr.RequiresPrescription = 1
where tp.PatientID = @PatientID

(FYI, if it helps to understand what I’m trying to do: I’m trying to identify if there are any TreatmentPlanDetail records for this Patient where the authorizing Payer requires a prescription for this ServiceType, but there is either no ServicePerscription record, or it has expired.)

Now, here’s what my C# code looks like:

var q = from tp in TreatmentPlans
        from tpd in tp.Details
        from auth in tpd.Authorizations
        join rx in ServicePrescriptions.DefaultIfEmpty() on tpd.ServiceTypeID equals rx.ServiceTypeID
        // from pstr in auth.Payer.ServiceTypeRules.DefaultIfEmpty() -- very frustrating that this doesn't work!!
        join pstr in LinqUtils.GetTable<PayerServiceTypeRules>().DefaultIfEmpty()
        on new { auth.PayerID, tpd.ServiceTypeID, RxReq = (bool)true } equals new { pstr.PayerID, pstr.ServiceTypeID, pstr.RequiresPrescription }
        select new { Payer = auth.Payer, Prescription = rx, TreatmentPlanDetail = tpd, Rules = pstr };

Oops, doesn’t compile! For some reason (I’d love an explanation) I can’t use that literal boolean inside the equijoin! Fine, I’ll leave it out, and filter out the “RequiresPrescription” stuff later…

...
join pstr in LinqUtils.GetTable<PayerServiceTypeRules>().DefaultIfEmpty()
on new { auth.PayerID, tpd.ServiceTypeID } equals new { pstr.PayerID, pstr.ServiceTypeID }
...

… and now it compiles – but when I run, I get an “Object reference not set” exception on this line. DUH! Of course there’s a null in there! How else are you supposed to perform a comparison with a left join, if you’re not allowed to reference the object on the right side, that might potentially be null?

So, how are you supposed to do a left join using multiple fields?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T01:14:38+00:00Added an answer on May 12, 2026 at 1:14 am

    I think you need to use the into keyword and resolve the missing children’s DefaultIfEmpty() after the join, not before:

    ...
    join pstr in LinqUtils.GetTable<PayerServiceTypeRules>()
    on new { auth.PayerID, tpd.ServiceTypeID, bool RequiresPrescription = true } 
    equals new { pstr.PayerID, pstr.ServiceTypeID, pstr.RequiresPrescription } 
    into pstrs
    from PSTR in pstrs.DefaultIfEmpty()
    select new { 
        Payer = auth.Payer, 
        Prescription = rx, 
        TreatmentPlanDetail = tpd, 
        Rules = PSTR 
    };
    

    LinqUtils.GetTable<PayerServiceTypeRules>().DefaultIfEmpty() is probably turning up a null because the DataTable returned contains no rows, thus causing your exception. Note the entire statement after in will be executed before selecting into it, which is not your desired behavior. You want the matching rows or null if no matching rows exist.


    For the boolean problem, it is a naming problem (nothing matches “RxReq” on the right side and nothing matches “RequiresPrescription” on the left side). Try naming the true “RequiresPrescription” as I have above (or name the right side’s pstr.RequiresPrescription “RxReq”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.