使用 linq 查询集合时,它总是返回 null

发布于 2024-09-25 17:22:45 字数 661 浏览 2 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸印温柔 2024-10-02 17:22:45

您的问题中至少有一处错别字。
您声明

oJCTransLabour = HttpContext.Current.Session["CurrentLabourTransactions"] as Collection<JCTransLabour>;

oJCTransLabour 之后不为空,对吗?如果会话包含它,这是有意义的。

然后定义 oCurrentLabourTrans 并为其分配 LINQ 查询。然后你说

// at this point the collection oJCTransLabour = null

等等,它怎么可能是空的?您甚至没有修改它(它是 oCurrentLabourTrans 修改的),您只是说 oJCTransLabour 不为空!

更新:我刚刚注意到名称中也有类型:您将其称为oJCTransLabours,然后将其称为oJCTransLabour。请发布准确代码,这样我们就不会假设您到底输错了什么。

当然,LINQ 表达式不能为 null。
我认为 oJCTransLabours 很可能为 null,尽管您另有说明。

您这么认为的原因也许是因为您确定 HttpContext.Current.Session["CurrentLabourTransactions"] 不为 null。这是正确的,但您使用 as 运算符将值转换为 Collection。与转换运算符相反,如果类型不匹配,as 不会引发异常,它只是返回 null

您可能想尝试

oJCTransLabour = (Collection<JCTransLabour>)HttpContext.Current.Session["CurrentLabourTransactions"];

看看是否出现任何错误。

There's at least one typo in your question.
You state that after

oJCTransLabour = HttpContext.Current.Session["CurrentLabourTransactions"] as Collection<JCTransLabour>;

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 that

// at this point the collection oJCTransLabour = null

Wait, how can it be null? You didn't even modify it (it was oCurrentLabourTrans modified) and you just said oJCTransLabour is not null!

Update: I just noticed there's type in names too: you call it oJCTransLabours, then you call it oJCTransLabour. 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 use as operator to cast the value to Collection<JCTransLabour>. As opposed to casting operator, as doesn't throw an exception if types mismatch, it just returns null.

You might want to try

oJCTransLabour = (Collection<JCTransLabour>)HttpContext.Current.Session["CurrentLabourTransactions"];

and see if any errors show up.

陌路终见情 2024-10-02 17:22:45

我认为您只是混淆了变量名称:

// You were missing an 's' before
oJCTransLabours = HttpContext.Current.Session["CurrentLabourTransactions"] 
                                    as Collection<JCTransLabour>;

I think you are just confusing variable names:

// You were missing an 's' before
oJCTransLabours = HttpContext.Current.Session["CurrentLabourTransactions"] 
                                    as Collection<JCTransLabour>;
寄人书 2024-10-02 17:22:45

这看起来不像 linq 问题,而是会话中的值要么为 null,要么不是 Collection

您可以通过将查找更改为确保它不为空:

oJCTransLabour = (HttpContext.Current.Session["CurrentLabourTransactions"] as Collection<JCTransLabour>) ?? new Collection<JCTransLabour>();

如果应该始终存在会话值,那么如果在执行查询之前未找到该值,则可以抛出异常。

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:

oJCTransLabour = (HttpContext.Current.Session["CurrentLabourTransactions"] as Collection<JCTransLabour>) ?? new Collection<JCTransLabour>();

If there should always be a session value then you could throw an exception if the value was not found before doing the query.

护你周全 2024-10-02 17:22:45

试试这个:

var oCurrentLabourTrans = (from clt in oJCTransLabours where clt.TransactionDate.Date != DateTime.Now.Date select clt).ToList();

try this:

var oCurrentLabourTrans = (from clt in oJCTransLabours where clt.TransactionDate.Date != DateTime.Now.Date select clt).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文