0

I've created a new Entity Object:

 mplToCreate = this._context.MasterPartsList.CreateOBject();

then, I want to set the parentPnID to an existing item's property that I tried querying for using the following IQueryable expression:

mplToCreate.parentPnID = this._context.MasterPartNumbers.FirstOrDefault(x => x.pn == this._selectedItem.Pn).pnID;

But I get a NullReferenceException at that line. (I know that there exists a pnID in MasterPartNumbers that matches this logic.)

Please help me correct my right-hand expression so that I can set the property accordingly.

Michael G
  • 6,695
  • 2
  • 41
  • 59
Rachael
  • 1,965
  • 4
  • 29
  • 55

1 Answers1

0

Is this a typo, or is it the problem?

// assignment
FirstOrDefault(x => x.pn = this._selectedItem.Pn)

// equality
FirstOrDefault(x => x.pn == this._selectedItem.Pn)
Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • Oops! Sorry @NicholasButler, that was just a typo. It's correct in my application. – Rachael Mar 13 '13 at 08:40
  • No problem. Your code looks correct - I would guess the problem is in your data. Which part of the expression is null? – Nick Butler Mar 13 '13 at 08:45
  • Ah. I'm tired. I just realized the SelectedItem is currently null in from inside my SaveEntities RelayCommand (they look like Josh Smith's). How should I pass the current property value of this to my RelayCommand? Would you happen to know? Thanks for your help. I'm pretty sure that will solve my problem. I'll start a new Q for that just in case. – Rachael Mar 13 '13 at 08:53
  • Yes, it is. Thanks anyway! – Rachael Mar 13 '13 at 09:09