简体   繁体   English

从两个不同的列表创建新的对象列表

[英]Create new list of objects from two different lists

I have two Lists of the same complex objects, but each list has different data for the objects.我有两个相同复杂对象的列表,但每个列表都有不同的对象数据。

I need to get a new set of lists of those same complex objects, but combining them for the count of the lesser objects.我需要获得一组相同复杂对象的新列表,但将它们组合起来计算较小对象的数量。 I am not sure how to do this.我不知道该怎么做。 I have tried to figure a way to union them, do addrange, and have tried using dictionaries instead, but this is a bit more complex than anything I have done before.我试图找到一种方法来联合它们,做 addrange,并尝试使用字典来代替,但这比我以前做过的任何事情都要复杂一些。 I have found similar answers, but none for my particular scenario.我找到了类似的答案,但没有针对我的特定情况。

Here is the best way I can sample out the code:这是我可以对代码进行采样的最佳方式:

List<Object1> ListA;
List<Object1> ListB;

ListA.count() = 5
ListB.count() = 3

new List<Object1> Object1CombinedList;

1. List<Object1> Object1CombinedList (Object1 (from listA), Object1 (from ListB))
2. List<Object1> Object1CombinedList (Object1 (from listA), Object1 (from ListB))
3. List<Object1> Object1CombinedList (Object1 (from listA), Object1 (from ListB))
4. List<Object1> Object1CombinedList (Object1 (from listA), null))
5. List<Object1> Object1CombinedList (Object1 (from listA), null))

I was thinking of somehow looping through the two independent lists to combine them for each pair as it went along.我正在考虑以某种方式循环遍历两个独立的列表,以便在进行时为每一对组合它们。 (I had thought I might be able to do that with dictionaries, but couldn't make it work). (我原以为我可以用字典来做到这一点,但无法让它发挥作用)。 The end result is, I need a new set of lists (again all of the same complex objects), equal to the count of the length of the longest of the two lists.最终结果是,我需要一组新的列表(同样是所有相同的复杂对象),等于两个列表中最长的长度的计数。

The new set needs to contain matched pairs from list A and List B until the smaller of the two sets is exhausted, BUT still contain a single complex object for the remainder of the longer list.新集合需要包含来自列表 A 和列表 B 的匹配对,直到两个集合中较小的一个用完,但对于较长列表的其余部分,仍然包含单个复杂对象。

The five lists shown above, are an example of what I need to get out of the two original source lists.上面显示的五个列表是我需要从两个原始源列表中获取的示例。 (with a sample of their data). (带有他们的数据样本)。

Hope that makes sense?希望这是有道理的? Asking this in the best manner I can.以最好的方式问这个问题。

Your question is a bit unclear, but I believe what you want is a ZipAll method.您的问题有点不清楚,但我相信您想要的是ZipAll方法。 Pseudo code:伪代码:

var listA = {1, 2, 3, 4}
var listB = {a, b, c}

ZipAll(listA, listB) == {(1, a), (2, b), (3, c), (4, null)}

Luckily, you can use a library called Sequences for this (disclosure: I'm the author).幸运的是,您可以为此使用一个名为Sequences的库(披露:我是作者)。

ISequence<Tuple<Object1, Object1>> zipped = listA.AsSequence().ZipAll(listB, null, null);

Disclaimer: I was writing this as @dcastro was commenting.免责声明:我是在@dcastro 发表评论时写这篇文章的。 I used a similar idea of a ZipAll method, but found from here我使用了ZipAll方法的类似想法,但从这里找到


What you are after is to zip both lists together, forming a new list along the way.您所要做的是将两个列表压缩在一起,从而形成一个新列表。 Unfortunately the built in Zip method stops when one of the lists finish, so an implementation which does not do so is what you need - commonly called ZipAll - can be found here不幸的是,当其中一个列表完成时,内置的Zip方法会停止,因此您需要一个不这样做的实现 - 通常称为ZipAll - 可以在这里找到

Then this becomes fairly straightforward:然后这变得相当简单:

var listA = new List<string>(){"a1","a2","a3","a4","a5"};
var listB = new List<string>(){"b1","b2","b3"};

var combined = listA.ZipAll(listB, (a,b) => new List<string>(){a,b}).ToList();
for(var i=0;i<combined.Count;i++)
{
    Console.WriteLine("{0}: {1} | {2}",i,combined[i][0],combined[i][1]);
}

The output is输出是

0: a1 | b1
1: a2 | b2
2: a3 | b3
3: a4 | 
4: a5 | 

Live example: http://rextester.com/PULLI83949现场示例: http : //rextester.com/PULLI83949

What about this那这个呢

            List<int> A = new List<int>() { 1, 2, 3 };
            List<int> B = new List<int>() { 1, 2, 3, 4 };

            List<int> C = A.Count > B.Count ? A : B;​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加入两个不同对象的列表并创建一个新列表 - Join two lists of different objects and create a new list 从两个索引列表中选择,创建新对象的列表 - Select from two indexing lists, create list of new objects 给定两个不同对象的列表,如何有效地将每个对象的值组合到一个基于公共 ID 的新列表中? - Given two lists of different objects, how to combine values from each into a new list based on a common Id, efficiently? 通过选择两个列表相等的对象来创建新列表 - create new list by selecting objects where two lists are equal C#采用两个不同对象的列表来构造新的对象列表 - C# taking two lists of different objects to construct new list of objects 如何压缩两个不同大小的列表以创建一个与原始列表中最长的列表大小相同的新列表? - How to Zip two Lists of different size to create a new list that is same as the size of the longest amongst the original lists? 比较2个列表并创建具有不同项目的新列表 - Compare 2 Lists and Create New List with Different Items 从 3 个不同的列表中获取匹配对象的列表 - Get list of matching objects from 3 different lists 从现有列表比较创建新列表 - Create New List From Existing Lists Comparison 从多个不同的列表创建通用列表 - Create a generic list from multiple different lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM