IEnumerable任务

发布于 2024-12-02 20:58:18 字数 859 浏览 5 评论 0原文

我有两个对象声明为 IEnumerable xVal 和 IEnumerable yVal。 然后我的程序在哪里:

var xVals = from p in result where p.chemical == "Benzene" select p.SampDate_Name;
var yVals = from p in result where p.chemical == "Benzene" select p.PCL;

然后我尝试执行以下分配:

 xVal = xVals as IEnumerable<DateTime>;
 yVal = yVals as IEnumerable<double>;

上面的代码xValyVal设置为null

有人能解释一下这里出了什么问题吗?我将非常感谢您的帮助。

谢谢。


有一点是确定的:输入不为空。因此,根据我的调试,xVals 和 yVals 是非空的。我还可以循环遍历它们以获取每个内容的内容。 当我回来并发布我的发现时,我会尝试其他建议。非常感谢大家的回复。

这里的问题是 LINQ 是惰性执行的,当你执行 foreach 或 ToList/ToArray/ToDictionary 时,它将被执行。我的代码中的两个 linq 查询在分配完成之前没有执行。因此,xValyVal 被设置为 null。 感谢您的所有帮助。

I have two objects declared as IEnumerable<DateTime> xVal and IEnumerable<double> yVal.
Then some where is my program I have:

var xVals = from p in result where p.chemical == "Benzene" select p.SampDate_Name;
var yVals = from p in result where p.chemical == "Benzene" select p.PCL;

Then I am trying to the following assignment:

 xVal = xVals as IEnumerable<DateTime>;
 yVal = yVals as IEnumerable<double>;

And the above code sets xVal and yVal to null.

Could anybody explain what is wrong here? I would very much appreciate the help.

Thanks.


One thing is certain: The input is not null. So xVals and yVals are non-null as debugged by me. I am also able to loop through them to get the content of each.
I will try the other suggestions when I get back and post my findings. Thanks a lot folks for your replies.

The problem here was that LINQ is executed lazy, It will be executed when you do either foreach or ToList/ToArray/ToDictionary. The two linq queries in my code were not executed before the assignment was done. Consequently xVal and yVal were set to null.
Thanks for all the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

夏日浅笑〃 2024-12-09 20:58:18

如果输入为 null 或无法执行强制转换,as 关键字会将输出设置为 null。

如果您使用 xVal = (IEnumerable)xVals 您可能会收到一个异常,指出无法进行转换。

as 关键字的 MSDN 说明

The as keyword will set the output to null if the input was null or the cast could not be carried out.

If you used xVal = (IEnumerable<DateTime>)xVals you would probably get an exception saying that it could not be cast.

MSDN description for as keyword

坏尐絯℡ 2024-12-09 20:58:18

如果您有一个通用集合,要将其获取到 IEnumerable,请尝试 .AsEnumerable()。所以你的新代码...

xVal = xVals.AsEnumerable();
yVal = yVals.AsEnumerable();

If you have a generic collection, to get it to IEnumerable, try .AsEnumerable(). So your new code...

xVal = xVals.AsEnumerable();
yVal = yVals.AsEnumerable();
柠檬色的秋千 2024-12-09 20:58:18

as 关键字实际上并不执行强制转换,而是执行相关引用类型之间的转换。因此,如果您有一个具体类型,您可以使用 as 将其分配给具有基类类型的新变量。如果类型不兼容,则 as 将返回 null。这是进行类型检查而不必担心异常的好方法。相反,您可以对转换结果执行空检查。

很难说出 p.SampDate_Name 和 p.PCL 是什么类型,但我猜字符串是什么类型?如果是这样,您可以将强制转换添加到 select 语句中。

var xVals = from p in result where p.chemical == "Benzene" select DateTime.Parse(p.SampDate_Name);
var yVals = from p in result where p.chemical == "Benzene" select Double.Parse(p.PCL);

The as keyword doesn't actually do a cast, but does conversions between related reference types. So if you have a concrete type, you could treat assign it to a new variable with a type of the base class using as. If the types are not compatible, then as will return null. This is a good way to do type checking without having to worry about exceptions. Instead you can perform a null check on the result of the conversion.

It's hard to tell what types p.SampDate_Name and p.PCL are, but I would guess strings? If so you can add the cast to the select statement.

var xVals = from p in result where p.chemical == "Benzene" select DateTime.Parse(p.SampDate_Name);
var yVals = from p in result where p.chemical == "Benzene" select Double.Parse(p.PCL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文