[英]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.