当使用linq查询集合时,它总是返回一个null [英] When querying a collection using linq it always returns a null

查看:269
本文介绍了当使用linq查询集合时,它总是返回一个null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  Collection< JCTransLabour> oJCTransLabours = null; 

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

//此时集合oJCTransLabours包含3500条记录

var oCurrentLabourTrans = from clt in oJCTransLabours
其中clt.TransactionDate.Date!= DateTime.Now。日期
select clt;

//此时集合oJCTransLabour = null
//我试图在不同的字段上搜索

return oCurrentLabourTrans;

必须有一些我做错了。任何帮助将非常感激。

解决方案

您的问题中至少有一个错字。

状态:

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

oJCTransLabour 不为null,对吗?



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

  //此时集合oJCTransLabour = null 

等待,它如何为null?你甚至没有修改它(它是 oCurrentLabourTrans 修改),你只是说 oJCTransLabour 不是null! p>

更新:我只注意到名称中也有类型:你叫它 oJCTransLabours 你叫它 oJCTransLabour 。请发布完全代码,这样我们就不会假定您输入的错误。



当然,LINQ表达式不能为null。
我认为最有可能的是, oJCTransLabours 是null,虽然你说明了否则。



原因你认为这可能是因为你确定 HttpContext.Current.Session [CurrentLabourTransactions] 不为null。这是真的,但您使用作为运算符将值转换为 Collection< JCTransLabour> 。与转换操作符相反, as 在类型不匹配时不抛出异常,它只返回 null p>

您可以尝试

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

并查看是否显示任何错误。


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

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.

这篇关于当使用linq查询集合时,它总是返回一个null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆