0

I have this piece of code:

int fundID = 28;

var investorData = from investor in db.Investors
                   join loanapp in db.LoanApplications
                       on investor.FundID equals loanapp.FundID into loanAppData
                   from lapp in loanAppData.DefaultIfEmpty()
                   join fundreport in db.FundReportLoanDatas
                       on lapp.LoanId equals fundreport.LoanId into fundReportData
                   from freport in fundReportData.DefaultIfEmpty()
                   where investor.FundID == fundID
                   select new InvestorPortfolioVM()
                   {
                       InvestorId = investor.InvestorID,
                       Investment = investor.FundFraction == 0 || freport.CurrentLB == null ? null : investor.FundFraction * (Double)freport.CurrentLB,
                       TotalLoan = freport.CurrentLB == null ? 0 : freport.CurrentLB,
                       PercFundsCommitted = investor.FundsCommitted == 0 || freport.CurrentLB == null ? null : freport.CurrentLB / investor.FundsCommitted,
                       PercFundsInvested = investor.FundsInvested == 0 || freport.CurrentLB == null ? null : freport.CurrentLB / investor.FundsInvested,
                       CurrentLTV = freport.CurrentLTV == null ? 0.0 : freport.CurrentLTV,
                       LoanId = lapp.LoanId,
                       SegLTV = freport.SegLTV == string.Empty ? "" : freport.SegLTV
                   };

Whenever I'm using the int variable fundID in the where clause I'm getting NullReferenceException, but when I'm writing directly where investor.FundID == 28 I'm able to get the results as expected.

What do I need to change in order to make the query work properly?

halfer
  • 19,824
  • 17
  • 99
  • 186
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • Read the marked duplicate, follow the advice. If you still can't figure it out, post a new post that includes a good [mcve] that reliably reproduces the problem, a detailed explanation of what you've done so far to debug the problem, and what _specifically_ you still can't figure out. – Peter Duniho Mar 03 '17 at 09:25
  • 1
    and if you rename variable from fundID to blaMagicValue? – Maksim Simkin Mar 03 '17 at 09:25
  • `investor.FundID` is `nullable` ? – Afnan Ahmad Mar 03 '17 at 09:26
  • 1
    Quite strange if that indeed happens exactly as you describe. You are sure there are no other things in play here? NullReference happens on that query line? – Evk Mar 03 '17 at 09:27
  • @AfnanAhmad no its not nullable – Laziale Mar 03 '17 at 09:27
  • @MaksimSimkin same nullreferenceexception outcome – Laziale Mar 03 '17 at 09:28
  • @Evk last line in that function is this one return Ok(investorData.ToList()); not sure if that changes anything. Thx for helping out! – Laziale Mar 03 '17 at 09:29
  • But did you attach debugger to figure out where it throws? Or you just call some web api in browser and see error in output? – Evk Mar 03 '17 at 09:30
  • @Evk I'm stepping in the debugger, and the error is thrown on the line where the int fundID = 28 resides. – Laziale Mar 03 '17 at 09:31
  • Well it cannot be thrown there :) Maybe there is some mess with debugging symbols, try to rebuild everything and try again. – Evk Mar 03 '17 at 09:32
  • I know and I'm surprised but not sure why its complaining at that line, although I'm sure that the error is somewhere below. – Laziale Mar 03 '17 at 09:33

0 Answers0