我需要从一个查询中获取我的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,int flags) + 0x1f bytes [Native to Managed Transition] [Managed to Native Transition] System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer,int flags) + 0x350 bytes System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer,int flags) + 0x1f bytes [Appdomain Transition]
解决方法
那么堆栈跟踪很有用:我假设你发布的代码在DoCalc()方法中.但是,您发布的行不能在代码中显示第5055行:
var myIDs = firstQuery.Select(cr => cr.IDs).ToList();
为了在这一行发生一个NullReferenceException,firstQuery将不得不为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.Room或myEntityContext.Cont中有一个null.看看他们的内容并验证这一点.如果您仍然不确定,当您收到异常时,请单击Visual Studio中的“将异常详细信息复制到剪贴板”(在异常窗口中),并将其粘贴到您的问题中.