0

我喜欢这个实体,

public class Receiving{
  [Key]
  public int ID {get; set;}
  public string shipperID {get; set;}

  [Foregign("shipperID")]
  public virtual Shipper shipper {get; set;}
}

托运人关系可以是1:0 或 1:1

当托运人为 0 时出现错误。

var result = from p in productRepository
             join o in receivingRepository
             on p.fk equals o.ID
             select new {
               test = o.shipper.name // if the shipper is nothing related then it occur an error.
             }

错误消息说, “对象引用未设置为对象的实例。”

如何在选择 {} 中检查?

我试过,

select new {
  test = o.shipper.name ?? ""
}

但它不工作。

4

1 回答 1

3

尝试(o == null || o.shipper == null) ? "" : o.shipper.name

于 2012-11-07T03:32:16.623 回答