dsLinq.Count() 引发了“System.NullReferenceException”类型的异常

Posted jeremy2001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dsLinq.Count() 引发了“System.NullReferenceException”类型的异常相关的知识,希望对你有一定的参考价值。

DataTable dt = PurchaseArriveInfoBus.GetPurchaseArriveInfo(companyCD, txtArriveNo, txtTitle, txtProvider);
        XElement dsXML = ConvertDataTableToXML(dt);
        //linq排序
        var dsLinq =
            (order == "ascending") ?
            (from x in dsXML.Descendants("Data")
             orderby x.Element(orderBy).Value ascending
             select new DataSourceModel()
             {
                 ID = x.Element("ID").Value,
                 ArriveNo = x.Element("ArriveNo").Value,
                 Title = x.Element("Title").Value,
                 ProviderID = x.Element("ProviderName").Value,
                 Purchaser = x.Element("Purchaser").Value,
                 ProductNo = x.Element("ProductNo").Value,
                 ProductName = x.Element("ProductName").Value,
                 ColorName = x.Element("ColorName").Value,
                 
                 TotalPrice = x.Element("TotalPrice").Value,
                 ProductCount = x.Element("ProductCount").Value,
                 InCount = x.Element("InCount").Value,
                 DetailID = x.Element("DetailID").Value,
                 UnitName = x.Element("UnitName").Value,
                 JiBenCount = x.Element("JiBenCount").Value,
                 UnitPrice = IsDisplayPrice ? x.Element("UnitPrice").Value : "0.000000"

             })
                      :
            (from x in dsXML.Descendants("Data")
             orderby x.Element(orderBy).Value descending
             select new DataSourceModel()
             {
                 ID = x.Element("ID").Value,
                 ArriveNo = x.Element("ArriveNo").Value,
                 Title = x.Element("Title").Value,
                 ProviderID = x.Element("ProviderName").Value,
                 Purchaser = x.Element("Purchaser").Value,
                 ProductNo = x.Element("ProductNo").Value,
                 ProductName = x.Element("ProductName").Value,
                 ColorName = x.Element("ColorName").Value,
                 
                 TotalPrice = x.Element("TotalPrice").Value,
                 ProductCount = x.Element("ProductCount").Value,
                 InCount = x.Element("InCount").Value,
                 DetailID = x.Element("DetailID").Value,
                 UnitName = x.Element("UnitName").Value,
                 JiBenCount = x.Element("JiBenCount").Value,
                 UnitPrice = IsDisplayPrice ? x.Element("UnitPrice").Value : "0.000000"
             });
        int totalCount = dsLinq.Count();

  dsLinq.Count() 引发了“System.NullReferenceException”类型的异常,其中任何字段都不能为null

以上是关于dsLinq.Count() 引发了“System.NullReferenceException”类型的异常的主要内容,如果未能解决你的问题,请参考以下文章

加载嵌入式 DLL 时引发 System.ExecutionEngineException

FileStream.ReadTimeout' 在 C# MVC 中引发了“System.InvalidOperationException”类型的异常

应用程序引发了未处理的异常。 System.ArgumentNullException:值不能为空。 (参数'uriString')

引发了“System.OutOfMemoryException”类型的异常。 C# 使用内存流时

使用 Graphics.GetHdc 时,System.Drawing.dll 引发了“System.ArgumentException”类型的未处理异常

aspose 单元格工作簿引发了“System.FormatException”类型的异常