I need to get my IDs (type Guid) from one query:
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;
}
Then I do some operation on it (updating the price), and finally I use the IDs for a second query. That second query doesn't contain these IDs. I implemented this problem in this way:
var myIDs = firstQuery.Select(cr => cr.IDs).ToList();
And my second query is:
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
};
When I run this code in debugger mode and reach this line:
var myIDs = firstQuery.Select(cr => cr.IDs).ToList();
...an exception is raised:
NullReferenceException
Object reference not set to an instance of an object.
It seems that it has something to do with the second query because when I transfer the second query to a separate method and pass IDs to it everything works perfectly, but I can't understand why it should consider some query which is written after the variable initializing.
The whole code is:
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();
Here is my stack trace, if it is useful:
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]
Exceptionin Visual Studio. Just copy it and add it to your post.