如何在LINQ子查询中处理Null?

Stu*_*art 3 linq vb.net null linq-to-sql

我有一个子查询,它返回子表中的最新值.在某些情况下,子查询不返回任何内容.下面的查询在运行时失败,因为MemberPrice的推断类型是十进制且不可为空.

简化查询:

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
Run Code Online (Sandbox Code Playgroud)

在SQL中,子查询将包含Top(1)并在空时返回Null.我怎样才能在LINQ中处理这个问题?有没有办法让MemberPrice可以为空,或者如果找不到则将值默认为零(或更优雅的解决方案)?

非常感谢,斯图尔特

Rob*_*vey 6

斯图尔特,

我将数据库中的Price字段更改为不允许空值,我得到了同样的错误:

"Operator '??' cannot be applied to operands of type 'decimal' and 'int'". 
Run Code Online (Sandbox Code Playgroud)

正如您所指出的,当Price设置为不允许数据库中的空值时,空合并运算符不再起作用,因为它期望看到可为空的类型十进制:

decimal?
Run Code Online (Sandbox Code Playgroud)

如果我删除空合并运算符并运行不包含价格的测试用例,我得到:

"The null value cannot be assigned to a member with type System.Decimal which is a non-nullable value type.."
Run Code Online (Sandbox Code Playgroud)

这是有效的代码.我将子查询结果转换为十进制?在应用空合并运算符之前.

public class Class1
{

    DataClasses1DataContext dc = new DataClasses1DataContext();

    public decimal test(int stockID)
    {

        var q = from s in dc.Stocks
                where s.StockID == stockID
                select new
                {
                    StockID = s.StockID,
                    memberPrice = ((decimal?)(from mp in dc.StockPrices
                                   where mp.StockID == s.StockID
                                   select mp.Price).FirstOrDefault()) ?? 0
                };

        return q.FirstOrDefault().memberPrice;
    }
}
Run Code Online (Sandbox Code Playgroud)