this is my first post here and I'm also new to C# / OO and LinqPAD. (translate to: I may mis-use concepts or terms due to being in 'learning mode')
I have an application that I've narrowed my bug down to the following query. I decided to try out LinqPAD to solve the issue in a constrained environment.
We are using Entity Framework for our database interactions. I am using LinqPAD's "C# Program" selection.
I found a great tutorial on how to use entities in LinqPAD and this seems to be working. (Proof: 'aEntities' does not return an error like it did when the entities were not connected properly)
So let's get to the issue.
At the 'var' I get: "NullReferenceException: Object reference not set to an instance of an object."
The reason I'm here asking this is every single example I see out there uses this 'var' approach. It does not work for some reason. I've tried declaring 'qryDC', creating a new instance of it and then assigning it. (I'm feeling weak knowledge wise here, but am reading and learning.)
I have read John Saunders epic write up about this topic. (Wow man! Thanks!) What is a NullReferenceException, and how do I fix it?
I however was not able to translate that knowledge to address my issue (learning curve).
Here is the code:
void Main(AEntities aEntities)
{
decimal fmProjectUID = 1123;
var qryDC =
from pnp in aEntities.PNonPRs
from p in aEntities.Projects
from pt in aEntities.PurchaseTypes
from wbs in aEntities.P32
from pc in aEntities.PPhases
where pnp.ProjectUID == p.ProjectUID
where p.ProjectUID == fmProjectUID
where pnp.PurchaseTypeUID == pt.PurchaseTypeUID
where pnp.P32UID == wbs.P32UID
where pt.IsNonPR == 1
orderby pt.PurchaseTypeID
select new
{
PNonPrUID = pnp.PNonPrUID
,PurchaseTypeID = pt.PurchaseTypeID
,PhaseCode = pnp.ProjectPhase.PhaseCode
,DANbr = pnp.AType.DANbr
,PreAmt = pnp.PreAmt
,POStDate = pnp.POStDate
,POFDate = pnp.POFDate
,BaseAmt = pnp.BaseAmt
,Notes = pnp.Notes
,ChangedByName = pnp.ChangedByName
};
}
Let me know if you need more info.
And thank you for your time, effort and thoughts.
You can't just arbitrarily assign arguments to the Main method and expect them to be filled in. Your aEntities
variable is null when you run this, thus the null reference exception. You need to instantiate your context.
void Main()
{
decimal fmProjectUID = 1123;
var aEntities = new AEntities();
var qryDC = from pnp in aEntities.PNonPRs
from p in aEntities.Projects
from pt in aEntities.PurchaseTypes
// etc...
}
Alternatively, if you defined your connection in LINQPad and then selected said connection for the query you can omit the creation of the AEntities and just use the following.
void Main()
{
decimal fmProjectUID = 1123;
var qryDC = from pnp in PNonPRs
from p in Projects
from pt in PurchaseTypes
// etc...
}
I would also suggest you familiarize yourself with the debugger in Visual Studio and LINQPad. Had you set a breakpoint and examined the variables you would have seen that aEntities
was null.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.