When querying a collection using linq it always returns a null
Collection<JCTransLabour> oJCTransLabours = null;
oJCTransLabour = HttpContext.Current.Session["CurrentLabourTransactions"] as
Collection<JCTransLabour>;
// at this point the collection oJCTransLabours contains 3500 records
var oCurrentLabourTrans = from clt in oJCTransLabours
where clt.TransactionDate.Date != DateTime.Now.Date
select clt;
// at this point the collection oJCTransLabour = null
// I have tried to search on different fields
return oCurrentLabourTrans;
There must be something I am doing wrong. Any help would be very much appreciated.
There’s at least one typo in your question.
You state that after
oJCTransLabouris not null, right? This makes sense if session contains it.Then you define
oCurrentLabourTransand assign LINQ query to it. And then you say thatWait, how can it be null? You didn’t even modify it (it was
oCurrentLabourTransmodified) and you just saidoJCTransLabouris not null!Update: I just noticed there’s type in names too: you call it
oJCTransLabours, then you call itoJCTransLabour. Please post the exact code so we don’t make assumptions about what exactly you might’ve mistyped.And, of course, LINQ expression can’t be null.
I think it is most likely that
oJCTransLaboursis null though you state the otherwise.The reason you think so, perhaps, is because you’re sure
HttpContext.Current.Session["CurrentLabourTransactions"]is not null. This is true, but you useasoperator to cast the value toCollection<JCTransLabour>. As opposed to casting operator,asdoesn’t throw an exception if types mismatch, it just returnsnull.You might want to try
and see if any errors show up.