简体   繁体   English

在 linq 查询中返回 null

[英]returning null in linq query

I fill the data from xml in the model我在 model 中填充来自 xml 的数据

Some xml tables don't have this(RequestedDeliveryDate,EarliestDeliveryDate,LatestDeliveryDate ) property一些 xml 表没有这个(RequestedDeliveryDate,EarliestDeliveryDate,LatestDeliveryDate)属性

For this reason return "Object reference not set to an instance of an object因此,返回“对象引用未设置为 object 的实例

Help me !!帮我 !!

SchedulingConditions = _detailInformation.Descendants("SchedulingConditions").Select(_schedulingConditions => new SchedulingConditionsModel
{
    SchedulingConditionList = _schedulingConditions.Descendants("SchedulingCondition").Select(_schedulingCondition => new SchedulingCondition
    {
        QuantityToBeDelivered = _schedulingCondition.Element("QuantityToBeDelivered").Value,
        DeliveryPlan = _schedulingCondition.Element("DeliveryPlan").Value,
        Frequency = _schedulingCondition.Element("Frequency").Value,
        RequestedDeliveryDate = _schedulingCondition.Element("RequestedDeliveryDate").Value, --> This property is return null
        EarliestDeliveryDate =  _schedulingCondition.Element("EarliestDeliveryDate").Value, --> This property is return null
        LatestDeliveryDate = _schedulingCondition.Element("LatestDeliveryDate").Value  --> This property is return null 
    }).ToList(),
}).FirstOrDefault(),

You can check if the property is null before trying to get value with "?".在尝试使用“?”获取值之前,您可以检查属性是否为 null。 If the property is null, the return is null, if it is not null, the return is the property value.如果属性是null,则返回null,如果不是null,则返回属性值。

SchedulingConditions = _detailInformation.Descendants("SchedulingConditions").Select(_schedulingConditions => new SchedulingConditionsModel
                            {

                                SchedulingConditionList = _schedulingConditions.Descendants("SchedulingCondition").Select(_schedulingCondition => new SchedulingCondition
                                {

                                    QuantityToBeDelivered = _schedulingCondition.Element("QuantityToBeDelivered").Value,
                                    DeliveryPlan = _schedulingCondition.Element("DeliveryPlan").Value,
                                    Frequency = _schedulingCondition.Element("Frequency").Value,
                                    RequestedDeliveryDate = _schedulingCondition.Element("RequestedDeliveryDate")?.Value, --> This property is return null
                                    EarliestDeliveryDate =  _schedulingCondition.Element("EarliestDeliveryDate")?.Value, --> This property is return null
                                    LatestDeliveryDate = _schedulingCondition.Element("LatestDeliveryDate")?.Value  --> This property is return null 

                                }).ToList(),

                            }).FirstOrDefault(),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM