I have a subquery that returns the most recent value from a child table. In some cases, the subquery returns nothing. The query below fails at runtime because the MemberPrice type is supposed to be decimal and not NULL.
Simplified request:
Dim q = From s In dc.STOCKs _
Select s.ID, MemberPrice = _
(From mp In dc.STOCKPRICEs Where mp.NUMBER = s.NUMBER _
Order By dc.date Descending _
Select mp.PRICE).FirstOrDefault
In SQL, the subquery will contain Top (1) and return Null if empty. How can I handle this in LINQ? Is there a way to make MemberPrice null, or the default value is null if not found (or a more elegant solution)?
Thanks a lot, Stuart
source
share