C# Unity3D : How to combine 2 elements of a list into Single elememnt.

Hello Everyone, I have a query if any one can help me out, it would be great.
I have 3 list in my application,

List 1 - contains elements with
public string Training, Test; ---- it acts as a pair.
list 1 :
a_training | a_test

List 2 - contains elements with
public string Name;
public float time;
public int count;

eg:
list 2 :
a_training | 12 | 2
a_test | 1.5 | 3

List 3 - contains elements
public string Name;
public float TimeTaken_Training;
public int Count_Training;
public float TimeTaken_Test;
public int Count_Test;

Expected Output for List 3 should be :


a | 12 | 2 | 1.5 | 3

I have to check whether it forms a pair or not, if no then display the data in particular training or test else if both present then in single line, as shown in expected output.

So, how can i get the expected output, I will trim the name but i want it in same line.

Why did you post the same thread twice?

I deleted that post mistakenly added it twice, this is the main thread.

Post is still there mate. Nonetheless the same concept I posted in the other thread applies here.

Loop through one list and build elements in the first list using, then loop through the second list and check for any matches with methods such as List<T>.Exists or List<T>.FindIndex.

Here’s some rough probably not correct code written in notepad++ to illustrate the idea:

public class ClassA
{
    public string Training;
 
    public string Test;
}

public class ClassB
{
    public string Name;
 
    public float Time;
 
    public int Count;
}

public class ClassC
{
    //constructors
    public ClassC(Class A)
    {
        //initialise values
    }
 
    public ClassC(Class B)
    {
        //initialise values
    }
 
    public string Name;
 
    public float TimeTaken_Training;
 
    public int Count_Training;
 
    public float TimeTaken_Test;
 
    public int Count_Test;
 
    public void Combine(ClassA classA)
    {
        //combine classA with existing values
    }
 
    public void Combine(ClassB classB)
    {
        //combine classB with existing values
    }
}

public List<ClassC> CombineLists(List<ClassA> classAs, List<ClassB> classBs)
{
    List<ClassC> classCs = new List<ClassC>();
 
    foreach (ClassA classA in classAs)
    {
        int index = classCs.FindIndex(x => x.Name.Contains(classA.Training)); //check if an element has a matching name or whatever
     
        if (index > -1)
        {
            classCs[index].Combine(classA);
        }
        else
        {
            classCs.Add(new ClassC(classA));
        }
    }
 
    foreach (ClassB classB in classBs)
    {
        int index = classCs.FindIndex(x => x.Name.Contains(classB.Name));
     
        if (index > -1)
        {
            classCs[index].Combine(classB);
        }
        else
        {
            classCs.Add(new ClassC(classB));
        }
    }

    return classCs;
}

And next time, use proper code examples, and posts like this should be in the scripting section.

This smells like a database query.

You need to take a look at Linq expressions.
https://stackoverflow.com/questions/14639481/merge-multiple-lists-into-one-list-with-linq
https://stackoverflow.com/questions/23016768/merge-two-listobject-into-one-list-in-linq
https://stackoverflow.com/questions/10637760/linq-group-by-and-select-collection

You also need to take a look at linq expressions. Usuaully you shouldn’t ever need to write a loop by hand for any sort of thing.

It smells like homework.

4 Likes

Its not homework, i was stuck on it so asked.

Guys, please stop replying to code scripting based questions in General Forum.

This is not support forum.
OP should know that by now, since it is long time user.

There are good use cases when you want that. And that is a performance.

Sorry, i have not used forum since long time, so i was not aware of it.

I’ll move this thread to the Scripting forum for you.

1 Like

It would be great, thank you.

Done.