C# 如果结果为空,如何返回空字符串 返回结果。选择(x=>new{ VillageName=x.VillageName, GroupID=x.GroupID, GroupName=x.GroupName, CenterID=x.CenterID, CenterName=x.CenterName, }).ToList();
您可以实现C#三元(?:)运算符。 三元运算符的语法为:C# 如果结果为空,如何返回空字符串 返回结果。选择(x=>new{ VillageName=x.VillageName, GroupID=x.GroupID, GroupName=x.GroupName, CenterID=x.CenterID, CenterName=x.CenterName, }).ToList();,c#,C#,您可以实现C#三元(?:)运算符。 三元运算符的语法为: return results.Select(x => new { VillageName = x.VillageName, GroupID = x.GroupID, GroupName = x.GroupName, CenterID = x.CenterID, CenterName
return results.Select(x => new {
VillageName = x.VillageName,
GroupID = x.GroupID,
GroupName = x.GroupName,
CenterID = x.CenterID,
CenterName = x.CenterName,
}).ToList<object>();
下面的表达式首先检查空条件,如果该条件为空,则该值设置为空
Condition ? Expression1 : Expression2;
你可以使用
如果中心名和组名为空。需要在c#6.0,x?中返回空字符串。CenterName->返回空值我假设
?
在这种情况下不适用于您<代码>x?.CenterName??“”我已将其添加为答案此代码运行良好。我甚至在使用同样的方法,但为什么人们不分析和测试我的答案就投票给我呢。加上对否决票的评论和你的理由会更好。是的,这个代码工作得很好。
return results.Select(x => new {
VillageName = x.VillageName,
GroupID = x.GroupID,
GroupName = x.GroupName == null ? "" : x.GroupName,
CenterID = x.CenterID,
CenterName = x.CenterName == null ? "" : x.CenterName,
}).ToList<object>();
CenterName = x.CenterName ?? "", //check and returns empty string if CenterName is null
x?.CenterName ?? string.Empty