使用 Linq 从 3 个集合创建项目
我有 3 个收藏品数量完全相同。
我需要根据这 3 个集合项值创建一个新集合。
示例:
List<double> list1;
List<double> list2;
List<double> list3;
List<Item> list4;
public class Item
{
public double Value1{get;set;}
public double Value2{get;set;}
public double Value3{get;set;}
}
我尝试使用 Linq 来实现此目的。
我尝试过:
var query = from pt in list1
from at in list2
from ct in list3
select new Item
{
Value1 = pt,
Value2 = at,
Value3 = ct
};
但是我遇到了 OutOfMemoryException,我的 3 个列表很大。
有什么帮助吗?
I've 3 collections with exactly the same items count.
I need to create a new collection based on these 3 collections item values.
Exemple :
List<double> list1;
List<double> list2;
List<double> list3;
List<Item> list4;
public class Item
{
public double Value1{get;set;}
public double Value2{get;set;}
public double Value3{get;set;}
}
I try to achieve this using Linq.
I tried :
var query = from pt in list1
from at in list2
from ct in list3
select new Item
{
Value1 = pt,
Value2 = at,
Value3 = ct
};
But i got a OutOfMemoryException, my 3 lists are huge.
Any help ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于您正在谈论
List
(它具有快速索引器),并且您保证所有三个列表的长度相同,因此最简单的方法是:这种方法显然获胜不适用于不支持快速索引器的集合。一种更通用的方法是编写一个
Zip3
方法,例如 F#Collections.Seq
模块附带的方法:Seq.zip3<'T1,'T2,'T3>
。否则,您可以链接两个Enumerable.Zip
一起调用以产生类似的行为(如其他答案中提到的),尽管这看起来确实很丑陋。Since you're talking about
List<T>
(which has a fast indexer), and you provide the guarantee that all three lists are of the same length, the easiest way would be:This approach obviously won't work well with collections that don't support fast indexers. A more general approach would be to write a
Zip3
method, such as the one that comes with the F#Collections.Seq
module:Seq.zip3<'T1,'T2,'T3>
. Otherwise, you could chain twoEnumerable.Zip
calls together to produce similar behaviour (as mentioned in other answers), although this does look quite ugly.您可以将它们压缩在一起 - 这是首先压缩 list2 和 list3,然后将组合列表与 list1 一起压缩:
You could zip them together - this is zipping up first list2 and list3, then zips the combined list together with list1:
将序列或列表映射到函数的参数的概念起源于LISP 编程语言,距今已有 50 多年历史。在 LISP 中,由于其非类型化和面向列表的性质,它是微不足道的。但是,用强类型语言做到这一点很困难,至少在解决将 n 个序列映射到带有 n 个参数的函数的一般问题方面是这样。
以下是一个可以满足大多数需求的微弱尝试:
The concept of mapping sequences or lists into the arguments of functions originated in the LISP programming language, a bit over 50 years ago. In LISP it is trivial because of its untyped and list-oriented nature. But, doing it in a strongly-typed language is difficult, at least in terms of solving the general problem of mapping n sequences to a function that takes n arguments.
Here's a feeble stab at what should accommodate most needs:
这是一个简化版本,它采用任意相同类型的序列(作为数组)并将它们压缩在一起:
优点
.Zip 的另一个重载()
方法.Zip
每次添加一个序列缺点
用法
Here is a simplified version which takes any number of sequences (as an array) of the same type and zips them together:
Pros
.Zip()
method.Zip
to add one more sequence each timeCons
Usage
有点破旧,但这应该可以。
a little shabby but this should work.
您可以尝试如下操作:
如果您总是要在列表上执行此操作,则可以将
.ElementAt(i)
与[i]
索引器一起放置。You can try something like the following:
If you're always going to be performing this on lists, you can place
.ElementAt(i)
with the[i]
indexer.如果您确定所有列表的大小相同,则应该使用 for 循环:
此解决方案非常简单且易于理解,不需要 LINQ。
If you are sure all the lists are of equal size you should use a for-loop:
This solution is very simple and easy to understand, no LINQ required.