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