Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by Mad_26, Oct 4, 2022.

  1. Mad_26

    Mad_26

    Joined:
    Apr 25, 2019
    Posts:
    15
    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 :

    <Training> <Test>
    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.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Why did you post the same thread twice?
     
  3. Mad_26

    Mad_26

    Joined:
    Apr 25, 2019
    Posts:
    15
    I deleted that post mistakenly added it twice, this is the main thread.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    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:
    Code (CSharp):
    1. public class ClassA
    2. {
    3.     public string Training;
    4.  
    5.     public string Test;
    6. }
    7.  
    8. public class ClassB
    9. {
    10.     public string Name;
    11.  
    12.     public float Time;
    13.  
    14.     public int Count;
    15. }
    16.  
    17. public class ClassC
    18. {
    19.     //constructors
    20.     public ClassC(Class A)
    21.     {
    22.         //initialise values
    23.     }
    24.  
    25.     public ClassC(Class B)
    26.     {
    27.         //initialise values
    28.     }
    29.  
    30.     public string Name;
    31.  
    32.     public float TimeTaken_Training;
    33.  
    34.     public int Count_Training;
    35.  
    36.     public float TimeTaken_Test;
    37.  
    38.     public int Count_Test;
    39.  
    40.     public void Combine(ClassA classA)
    41.     {
    42.         //combine classA with existing values
    43.     }
    44.  
    45.     public void Combine(ClassB classB)
    46.     {
    47.         //combine classB with existing values
    48.     }
    49. }
    50.  
    51. public List<ClassC> CombineLists(List<ClassA> classAs, List<ClassB> classBs)
    52. {
    53.     List<ClassC> classCs = new List<ClassC>();
    54.  
    55.     foreach (ClassA classA in classAs)
    56.     {
    57.         int index = classCs.FindIndex(x => x.Name.Contains(classA.Training)); //check if an element has a matching name or whatever
    58.      
    59.         if (index > -1)
    60.         {
    61.             classCs[index].Combine(classA);
    62.         }
    63.         else
    64.         {
    65.             classCs.Add(new ClassC(classA));
    66.         }
    67.     }
    68.  
    69.     foreach (ClassB classB in classBs)
    70.     {
    71.         int index = classCs.FindIndex(x => x.Name.Contains(classB.Name));
    72.      
    73.         if (index > -1)
    74.         {
    75.             classCs[index].Combine(classB);
    76.         }
    77.         else
    78.         {
    79.             classCs.Add(new ClassC(classB));
    80.         }
    81.     }
    82.  
    83.     return classCs;
    84. }
    And next time, use proper code examples, and posts like this should be in the scripting section.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    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.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    It smells like homework.
     
  7. Mad_26

    Mad_26

    Joined:
    Apr 25, 2019
    Posts:
    15
    Its not homework, i was stuck on it so asked.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,592
    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.
     
  9. Mad_26

    Mad_26

    Joined:
    Apr 25, 2019
    Posts:
    15
    Sorry, i have not used forum since long time, so i was not aware of it.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    I'll move this thread to the Scripting forum for you.
     
    Antypodish likes this.
  11. Mad_26

    Mad_26

    Joined:
    Apr 25, 2019
    Posts:
    15
    It would be great, thank you.
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Done.