LINQ foreach在列表创建期间(LINQ foreach during list creation)

编程入门 行业动态 更新时间:2024-10-10 13:16:53
LINQ foreach在列表创建期间(LINQ foreach during list creation)

我有一个简单的LINQ语句,它构建了一个大约80亿个项目的庞大怪物列表。 然后循环每个项目并对该项目执行操作。 问题是,在完全创建列表之前,foreach甚至不会运行。 是否可以使用LINQ并让它为每个项目执行foreach,因为它构建它而不是等待?

我没有在列表中做任何事情,比如排序。 只是直接列表建设。

编辑:这可能看起来很奇怪,但我有一些数据库行有几个数值。 我知道这些值的一些算术组合产生了我想要的数字,但我不确定是哪一个。 因此我创建了以下LINQ:

( from I1 in Items from I2 in Items from I3 in Items from I4 in Items select new Item[] { I1, I2, I3, I4 } ).ToList().ForEach(x => CalculateValues(x);

我做了上面的声明,1项,然后2项,依此类推。 我的电脑在3件物品上挂了一段时间,约有2800万件物品。

我知道这是低效的,但我打算做一个快速的程序来过夜。

I have a simple LINQ statement that builds a huge monster list of around 8 billion items. Each of the items is then looped through and an operation is performed on the item. The problem is that the foreach is not even run until the list is fully created. Is it possible to use LINQ and have it execute the foreach for each item as it builds it instead of waiting?

I am not doing anything in the list like sorting. Just straight list building.

Edit: This may seem wierd, but I have some database rows with several numeric values. I know that some arithmetic combination of those values yields a number I want, but am not sure which. Thus I created the following LINQ:

( from I1 in Items from I2 in Items from I3 in Items from I4 in Items select new Item[] { I1, I2, I3, I4 } ).ToList().ForEach(x => CalculateValues(x);

I did the above statement for 1 item then 2 items and so on. My computer hung for a while at the 3 items with is about 28 million items.

I do know that this is inefficient, but I was going to do a quick program to run overnight.

最满意答案

不要调用ToList()方法将尝试将整个数据加载到内存中

尝试这个,

var q = from I1 in Items from I2 in Items from I3 in Items from I4 in Items select new Item[] { I1, I2, I3, I4 }; foreach(var x in q) { CalculateValues(x); }

对于您的输入可能仍然很慢,但如果我是正确的,它将流式传输结果并一次处理一个。

Don't call ToList() that method will try to load the whole data in memory

Try this,

var q = from I1 in Items from I2 in Items from I3 in Items from I4 in Items select new Item[] { I1, I2, I3, I4 }; foreach(var x in q) { CalculateValues(x); }

that might still be slow for your input, but it will stream the results and process them one at a time if I am correct.

更多推荐

本文发布于:2023-08-07 18:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   foreach   LINQ   list   creation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!