firstordefault之后如何检查空值?

时间:2018-07-31 08:34:30

标签: c# linq

var data = (from x in simpMeds.Descendants("PharmacyReviewer")
    select x.Element("DisplayName").Value.ToString()).FirstOrDefault();

1 个答案:

答案 0 :(得分:0)

var data = (from x in simpMeds.Descendants("PharmacyReviewer")
    select x.Element("DisplayName").Value.ToString()).FirstOrDefault()

必须选中null才能获取数据属性。即:

var property = data?.Property1; // property == null if data == null

必须选中null才能选择另一个对象。即:

var myValue = data ?? otherData; // myValue == otherData if data == null

或者简单地

if(data == null)
  //do something
else
 //do other thing