我需要从一个查询中获取我的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
对象引用未设置为对象的实例。
似乎与第二个查询有关,因为当我将第二个查询转移到单独的方法中并传递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]该文本是一个堆栈跟踪,它显示了在运行代码时发生错误的位置。具体来说,它显示了在哪个方法中发生错误,以及在调用该方法的时候有哪些上下文和参数。
foreach
循环可能会引起问题。你为什么不在select new
中将Counts.Price
分配给CountPrice
和RoomPrice
呢? - Dzienny