【问题标题】:Object reference not set error if linq query doesn't find a matching record? [duplicate]如果 linq 查询未找到匹配记录,则对象引用未设置错误? [复制]
【发布时间】:2019-04-14 12:08:22
【问题描述】:

我有这个代码

public object GetMaxReportNo(string OfficeStationCombination = "")
        {
            try
            {
                InspectionReport InspectionReport = new InspectionReport();
                string VelosiReportNo = "";

                var query = uow.InspectionReportRepository.GetQueryable().AsQueryable();

                if (query.Any())
                {
                    VelosiReportNo = query.Where(x => x.VelosiReportNo.Contains(OfficeStationCombination)).OrderByDescending(x => x.InspectionReportID).DefaultIfEmpty(null).FirstOrDefault().VelosiReportNo;
                }

                return VelosiReportNo;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

这一行:

VelosiReportNo = query.Where(x => x.VelosiReportNo.Contains(OfficeStationCombination)).OrderByDescending(x => x.InspectionReportID).DefaultIfEmpty(null).FirstOrDefault().VelosiReportNo;

抛出错误:

对象引用未设置为对象的实例。

当我传递数据库中尚不存在记录的参数时。但是,我无法弄清楚它返回的是什么?我该如何控制它?

我已经处理了null,但这不起作用。如果没有找到记录,我该如何处理,以便我可以据此做出决定?

【问题讨论】:

  • 检查对象是否不为空,然后使用它的属性。
  • 你试图在一行代码中做太多事情。分解碎片并沿途检查是否为空。

标签: c# asp.net-mvc entity-framework linq entity-framework-6


【解决方案1】:
query.Where(x => x.VelosiReportNo.Contains(OfficeStationCombination)).Any()

成功了。

完整代码;

                if (query.Any())
                {
                    if (query.Where(x => x.VelosiReportNo.Contains(OfficeStationCombination)).Any())
                    {
                        VelosiReportNo = query.Where(x => x.VelosiReportNo.Contains(OfficeStationCombination)).OrderByDescending(x => x.InspectionReportID).FirstOrDefault().VelosiReportNo;
                    }
                    else
                    {
                        VelosiReportNo = null;
                    }

                }

【讨论】:

    【解决方案2】:

    你好发布问题,

    要解决您的问题,您需要在尝试从中检索属性值之前检查结果是否不为空。

    你可以检查下面的代码没什么神奇的:

    1. 我禁止了 Try/Catch,因为没有兴趣捕捉和重新抛出它。
    2. 我禁止 DefaultIfEmpty 因为字符串的默认值为 null。
    3. 我添加了一个检查结果是否为空。

      public object GetMaxReportNo(string OfficeStationCombination = "")
      {
              InspectionReport InspectionReport = new InspectionReport();
              string VelosiReportNo = "";
      
              var query = uow.InspectionReportRepository.GetQueryable().AsQueryable();
      
              if (query.Any())
              {
                  var queryResult = query.Where(x => x.VelosiReportNo.Contains(OfficeStationCombination))
                                        .OrderByDescending(x => x.InspectionReportID)
                                        .FirstOrDefault();
                  if (queryResult != null)
                      VelosiReportNo = queryResult.VelosiReportNo;
              }
      
              return VelosiReportNo;
      }
      

    【讨论】:

    • 这实际上不起作用。我试过了。还是谢谢
    • @PostingQuestions 是什么错误?
    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多