简体   繁体   中英

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. 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.

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. 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).

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

Disclaimer: I was writing this as @dcastro was commenting. I used a similar idea of a ZipAll method, but found from here


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

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

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;​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM