gpt4 book ai didi

c# - 如何将 linq 中的两列连接到 sql 查询的选择投影

转载 作者:行者123 更新时间:2023-11-30 15:00:14 24 4
gpt4 key购买 nike

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

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

6- 我尝试了以下方法,但没有用。仍然产生 null。

from s in test
select new{
Phone = s.PhoneCountryCode == null ? "" : s.PhoneCountryCode + s.PhoneNumber == null ? "" : s.PhoneNumber + s.PhoneExtension == null ? "" : s.PhoneExtension
}

最佳答案

您可以使用 ??运算符如下:

from s in test
select new
{
Phone = (s.PhoneCountryCode ?? "") + (s.PhoneNumber ?? "") + (s.PhoneExtension ?? "")
}

关于c# - 如何将 linq 中的两列连接到 sql 查询的选择投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15683426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com