【发布时间】:2020-01-23 13:25:57
【问题描述】:
在我的选择语句中,我想检查 null 或空。
[HttpGet("service")]
public IActionResult GetService()
{
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile("project.conf");
IKubernetes client = new Kubernetes(config);
var volumeList = client.ListNamespacedService("default");
var result = from item in volumeList.Items
select new
{
MetadataName = item.Metadata.Name,
Namespace = item.Metadata.NamespaceProperty,
Age = item.Metadata.CreationTimestamp,
Type = item.Spec.Type,
All = item.Status,
Ip = item.Status.LoadBalancer.Ingress.Select(x => x.Ip)
};
return Ok(result);
}
Json 结果是:
{
"metadataName": "cred-mgmt-redis-slave",
"namespace": "default",
"age": "2019-12-20T09:50:11Z",
"type": "ClusterIP",
"all": {
"loadBalancer": {
"ingress": null
}
}
},
{
"metadataName": "jenkins",
"namespace": "default",
"age": "2020-01-01T16:38:58Z",
"type": "LoadBalancer",
"all": {
"loadBalancer": {
"ingress": [
{
"hostname": null,
"ip": "185.22.98.93"
}
]
}
}
}
我知道在我的情况下 ingress 是空的,在这种情况下我得到空引用异常。我需要检查入口,如果它不为空,则显示 ip。
【问题讨论】:
-
你试过用'?.'句法?例如item.Status.LoadBalancer?.Ingress.Select(x => x.Ip)
标签: c# json asp.net-core