c# - 有效操作时出现 NullReferenceException

标签 c# linq entity-framework

我需要从一个查询中获取我的 ID(类型 Guid):

var firstQuery = 
    from rooms in myEntityContext.Room.Where(t => t.fldClosed == 0)
    join conts in myEntityContext.Cont on rooms.ID equals conts.ItemID
    select new
    {
        ContPrice = conts.Price,
        RoomPrice = rooms.Price
        IDs = rooms.ID
    };

foreach (var t in firstQuery)
{
    t.RoomPrice  = t.ContPrice;
}

然后我对其进行一些操作(更新价格),最后我使用这些 ID 进行第二次查询。第二个查询不包含这些 ID。我以这种方式实现了这个问题:

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();

我的第二个查询是:

var secondQuery = 
    from rooms in myEntityContext.Room.Where(t => t.fldClosed == 0) 
    where !myIDs.Contains(rooms.fldID)                                   
    join conts in myEntityContext.Cont on rooms.ID equals conts.ItemID
    select new
    {
       RoomPrice = conts.fldPrice,
       IDs = rooms.ID
    };

当我在调试器模式下运行这段代码并到达这一行时:

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();

...引发异常:

NullReferenceException
Object reference not set to an instance of an object.

它似乎与第二个查询有关,因为当我将第二个查询转移到一个单独的方法并将 ID 传递给它时,一切正常,但我不明白为什么它应该考虑一些已编写的查询变量初始化后。

整个代码是:

var calcDate = DateTime.Now.AddDays(-1);

var firstQuery = 
    from rooms in myEntityContext.Room.Where(t => t.fldClosed == 0)
    join conts in myEntityContext.Cont on rooms.ID equals conts.ItemID
    where conts.date == calcDate
    select new
    {
        ContPrice = conts.Price,
        RoomPrice = rooms.Price
        IDs = rooms.ID
    };

foreach (var t in firstQuery)
{
    t.RoomPrice = t.ContPrice;
}

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();


var secondQuery = 
    from rooms in myEntityContext.Room.Where(t => t.fldClosed == 0) 
    where !myIDs.Contains(rooms.fldID)                                   
    join conts in myEntityContext.Cont on rooms.ID equals conts.ItemID
    where conts.date == calcDate && conts.Code = "01"
    select new
    {
       RoomPrice = conts.fldPrice,
       IDs = rooms.ID
    };

foreach (var t in secondQuery)
{
    ContPrice = Conts.Price,
    RoomPrice = Rooms.Price
}

myEntityContext.SaveChanges();

这是我的堆栈跟踪,如果有用的话:

Financial.UI.dll!Financial.UI.Services.Hotels.HotelServiceProxy.CalcProxy.DoCalc(System.DateTime calcDate) Line 5055    C#
Financial.UI.dll!Financial.UI.Pages.Hotel.NightsCalculationPage.CallbackMethod_DoCalc() Line 65 + 0x37 bytes    C#
[Native to Managed Transition]  
Web.UI.dll!Web.UI.SystemCallback.ProcessCallback() Line 228 + 0x3b bytes    C#
Web.UI.dll!Web.UI.SystemCallbackHandler.ProcessRequest(System.Web.HttpContext context) Line 68 + 0x12 bytes C#
System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x156 bytes    
System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step, ref bool completedSynchronously) + 0x46 bytes 
System.Web.dll!System.Web.HttpApplication.PipelineStepManager.ResumeSteps(System.Exception error) + 0x342 bytes 
System.Web.dll!System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext context, System.AsyncCallback cb) + 0x60 bytes 
System.Web.dll!System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest wr, System.Web.HttpContext context) + 0xbb bytes   
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f3 bytes   
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f bytes  
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x350 bytes   
System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f bytes  
[Appdomain Transition]  

最佳答案

好吧,堆栈跟踪有用的:我假设您发布的代码是在 DoCalc() 方法中。但是,您发布的行不可能是代码中的第 5055 行:

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();

要在该行发生 NullReferenceExceptionfirstQuery 必须为 null 或 firstQuery.Select(cr => cr.IDs) 必须返回 null...

但是,如果是这种情况,您在这两种情况下都会得到 ArgumentNullException不会 NullReferenceException。所以这不是错误所在的行。


此外,您的代码甚至不应该运行!

例如,在下面的片段中你应该得到一个编译时错误:

foreach (var t in firstQuery)
{
    t.RoomPrice = t.ContPrice;
}

Property or indexer 'AnonymousType#1.RoomPrice' cannot be assigned to -- it is read only

你想在这里做什么......我不知道:

foreach (var t in secondQuery)
{
    ContPrice = Conts.Price,
    RoomPrice = Rooms.Price
}

最有可能发生的事情

您在 myEntityContext.RoommyEntityContext.Cont 中有一个 null。查看他们的内容并验证这一点。如果您仍然不确定,当您获得异常时,请在 Visual Studio 中(在异常窗口上)单击“将异常详细信息复制到剪贴板”并将其粘贴到您的问题中。

关于c# - 有效操作时出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14956263/

相关文章:

c# - LINQ to Entities 不支持扩展方法?

c# - 配置 WPF 客户端以运行 64 位

c# - 动态生成具有嵌套属性的 LINQ select

c# - LINQ to Entities 无法识别方法 'System.String ToString()' 方法并且此方法无法转换为存储表达式

c# - 工作 Linq 查询但性能低

c# - Windows XP/Windows 7 和 Windows Servers 登录页面

c# - 方法 'System.Linq.Queryable.SelectMany System.Linq.IQueryable 的类型参数

sql-server - 在 C# Visual Studio 数据库中查询特定表

c# - Entity Framework : how to delete related entity upon update or delete of 'generic' parent

java - Android viewPager使用本地html内存不足