使用 linq 查询集合时,它总是返回 null
使用 linq 查询集合时,它总是返回 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;
一定是我做错了什么。任何帮助将非常感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的问题中至少有一处错别字。
您声明
oJCTransLabour
之后不为空,对吗?如果会话包含它,这是有意义的。然后定义
oCurrentLabourTrans
并为其分配 LINQ 查询。然后你说等等,它怎么可能是空的?您甚至没有修改它(它是
oCurrentLabourTrans
修改的),您只是说oJCTransLabour
不为空!更新:我刚刚注意到名称中也有类型:您将其称为
oJCTransLabours
,然后将其称为oJCTransLabour
。请发布准确代码,这样我们就不会假设您到底输错了什么。当然,LINQ 表达式不能为 null。
我认为
oJCTransLabours
很可能为 null,尽管您另有说明。您这么认为的原因也许是因为您确定
HttpContext.Current.Session["CurrentLabourTransactions"]
不为 null。这是正确的,但您使用as
运算符将值转换为Collection
。与转换运算符相反,如果类型不匹配,as
不会引发异常,它只是返回null
。您可能想尝试
看看是否出现任何错误。
There's at least one typo in your question.
You state that after
oJCTransLabour
is not null, right? This makes sense if session contains it.Then you define
oCurrentLabourTrans
and assign LINQ query to it. And then you say thatWait, how can it be null? You didn't even modify it (it was
oCurrentLabourTrans
modified) and you just saidoJCTransLabour
is 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
oJCTransLabours
is 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 useas
operator to cast the value toCollection<JCTransLabour>
. As opposed to casting operator,as
doesn't throw an exception if types mismatch, it just returnsnull
.You might want to try
and see if any errors show up.
我认为您只是混淆了变量名称:
I think you are just confusing variable names:
这看起来不像 linq 问题,而是会话中的值要么为 null,要么不是
Collection
。您可以通过将查找更改为确保它不为空:
如果应该始终存在会话值,那么如果在执行查询之前未找到该值,则可以抛出异常。
This doesn't look like a linq issue, but rather that the value in the session is either null or not a
Collection<JCTransLabour>
.You could ensure it is not null by changing the lookup to:
If there should always be a session value then you could throw an exception if the value was not found before doing the query.
试试这个:
try this: