我需要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;
}
然后我对其进行一些操作(更新价格),最后我使用 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) 第 5055 行 C# Financial.UI.dll!Financial.UI.Pages.Hotel.NightsCalculationPage.CallbackMethod_DoCalc() 第 65 行 + 0x37 字节 C# [本机到托管转换] Web.UI.dll!Web.UI.SystemCallback.ProcessCallback() 第 228 行 + 0x3b 字节 C# Web.UI.dll!Web.UI.SystemCallbackHandler.ProcessRequest(System.Web.HttpContext context) 第 68 行 + 0x12 字节 C# System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x156 字节 System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step, ref bool completedSynchronously) + 0x46 字节 System.Web.dll!System.Web.HttpApplication.PipelineStepManager.ResumeSteps(System.Exception 错误) + 0x342 字节 System.Web.dll!System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext context, System.AsyncCallback cb) + 0x60 字节 System.Web.dll!System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest WR, System.Web.HttpContext context) + 0xbb 字节 System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f3 字节 System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f 字节 [本机到托管转换] [管理到本地转换] System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x350 字节 System.Web.dll!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(System.IntPtr rootedObjectsPointer, System.IntPtr nativeRequestContext, System.IntPtr moduleData, int flags) + 0x1f 字节 [Appdomain 过渡]