如何在linq to sql查询的选择投影中连接两个列

3

1- 我使用linq to sql查询数据库表。
2- 在我的实际表中,我将电话国家代码、电话号码和电话分机存储在不同的列中。
3- 当我获取数据时,我需要电话等于电话国家代码、电话号码和电话分机的连接。
4- 对于某些记录,这3个列中的任何一个可能具有空值。
5- 如果其中一个列为空,则整个连接结果为null。

from s in test
select new{
          Phone = s.PhoneCountryCode + s.PhoneNumber + s.PhoneExtension
}

我尝试了以下方法,但没有成功。仍然返回 null 值。
from s in test
select new{
          Phone = s.PhoneCountryCode == null ? "" : s.PhoneCountryCode + s.PhoneNumber       == null ? "" : s.PhoneNumber + s.PhoneExtension == null ? "" : s.PhoneExtension
} 
1个回答

5
你可以按照以下方式使用 ?? 运算符:
from s in test
select new
{
    Phone = (s.PhoneCountryCode ?? "") + (s.PhoneNumber ?? "") + (s.PhoneExtension ?? "")
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接