【发布时间】:2010-11-09 12:32:48
【问题描述】:
我正在返回一个匿名类:
var clients = from c in this.ClientRepository.SearchClientByTerm(term, 10)
select new
{
id = c.Id,
line1 = c.Address.Line1 ?? "Unknown Information ..."
};
问题是地址可以为空,这意味着如果它为空,它会爆炸成一百万个。
我能想到的最优雅的解决方案是这个......
line1 = c.Address != null && c.Address.Line1 != null
? c.Address.Line1 : "Unknown Information ..."
有更好的方法吗?我不喜欢失去使用空合并运算符然后不得不检查是否为空的能力。
【问题讨论】:
-
我使用的是 poco,所以如果为空,我将地址设置在那里。谢谢。
标签: c# null-coalescing-operator