我有一个简单的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.
更多推荐
发布评论