有效操作的NullReferenceException

Mar*_*shi 9 c# linq entity-framework

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

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;
}
Run Code Online (Sandbox Code Playgroud)

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

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();
Run Code Online (Sandbox Code Playgroud)

我的第二个问题是:

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
    };
Run Code Online (Sandbox Code Playgroud)

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

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();
Run Code Online (Sandbox Code Playgroud)

......提出了一个例外:

NullReferenceException
未将对象引用设置为对象的实例.

它似乎与第二个查询有关,因为当我将第二个查询转移到一个单独的方法并将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();
Run Code Online (Sandbox Code Playgroud)

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

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]  

Dan*_*ker 5

那么,堆栈跟踪有用的:我认为您发布的代码是在DoCalc()方法.但是,您发布的行不能在代码中为5055行:

var myIDs = firstQuery.Select(cr => cr.IDs).ToList();
Run Code Online (Sandbox Code Playgroud)

NullReferenceException在此行上发生,要么firstQuery必须为null ,要么firstQuery.Select(cr => cr.IDs)必须返回null ...

但是,如果是这种情况,你会ArgumentNullException在两种情况下得到一个,而不是一个NullReferenceException.所以这不是错误的界限.


此外,你的代码甚至没有运行!

例如,在下面的部分中,您应该得到编译时错误:

foreach (var t in firstQuery)
{
    t.RoomPrice = t.ContPrice;
}
Run Code Online (Sandbox Code Playgroud)

属性或索引器'AnonymousType#1.RoomPrice'无法分配 - 它是只读的

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

foreach (var t in secondQuery)
{
    ContPrice = Conts.Price,
    RoomPrice = Rooms.Price
}
Run Code Online (Sandbox Code Playgroud)

最有可能发生的事情

你有一个nullin myEntityContext.Room或in myEntityContext.Cont.查看他们的内容并验证这一点.如果您仍然不确定,当您收到异常时,请在Visual Studio中单击"将异常详细信息复制到剪贴板"(在异常窗口中)并将其粘贴到您的问题中.


quj*_*jck 2

我假设var FirstQuery解析为 IEnumerable< out T >。

Enumerable您不能多次访问 a 的内容。

尝试在第一个查询中创建一个列表:

var FirstQuery = (from rooms in myEntityContext.Room.Where(t => t.fldClosed == 0)                                    
    join Conts in myEntityContextt.Cont on rooms.ID equals Conts.ItemID
    select new
    {
        RoomPrice = Conts.fldPrice,
        IDs = rooms.ID
    }).ToList();
Run Code Online (Sandbox Code Playgroud)

如果此行失败,则 myEntityContext.Mark 或 myEntityContext.Cont 可能是 null 对象