I asked this question a week ago and now I found something that I missed.I didnt know if I will get any replies if I reopen the question that's why i created a new one. The last condition doesn't work. I am trying to iterate in two list with different objects.I am adding the records to a third list if they complete the following conditions when I compare listTest1 and listTest2
- If the abbrv and the date is the same I am adding the object from listTest2 to listTest3
- If the abbrv is the same but the date is different I am adding the object from listTest2 to listTest3 and I am switching the completed property to true. In addition, I am adding the record from listTest1 to listTest3.
- If the abbrv from listTest1 doesn't exist in listTest2 I am adding the record from listTest1 to listTest3.
- If the abbrv from listTest2 doesnt exist in listTest2. I am adding the record from listTest2 to listTest2
I know that it sounds quite confusing that is why I will show you what I am getting and what I am expecting.
I am getting, The test 6 object is missing
class Program
{
static void Main(string[] args)
{
Test1 test1 = new Test1() { abbrv = "Test1", date = new DateTime(2017, 11, 12), completed = false };
Test1 test2 = new Test1() { abbrv = "Test2", date = new DateTime(2017, 12, 17), completed = false };
Test1 test5 = new Test1() { abbrv = "Test5", date = new DateTime(2017, 12, 12), completed = false };
Test2 test3 = new Test2() { abbrv = "Test1", date = new DateTime(2017, 11, 12), completed = false, abbrevName = "AbbrvName1" };
Test2 test4 = new Test2() { abbrv = "Test2", date = new DateTime(2017, 12, 12), completed = false, abbrevName = "AbbrvName2" };
Test2 test7 = new Test2() { abbrv = "Test5", date = new DateTime(2017, 12, 18), completed = false, abbrevName = "AbbrvName3" };
Test2 test6 = new Test2() { abbrv = "Test6", date = new DateTime(2017, 12, 18), completed = false, abbrevName = "AbbrvName3" };
List<Test1> listTest1 = new List<Test1>();
List<Test2> listTest2 = new List<Test2>();
List<Test2> listTest3 = new List<Test2>();
listTest1.Add(test1);
listTest1.Add(test2);
listTest1.Add(test5);
listTest2.Add(test6);
listTest2.Add(test3);
listTest2.Add(test4);
listTest2.Add(test7);
for (int i = 0; i < listTest1.Count; i++)
{
bool abbrvFound = false;
for (int a = 0; a < listTest2.Count; a++)
{
if (listTest1[i].abbrv != listTest2[a].abbrv)
{
continue;
}
abbrvFound = true;
if (listTest1[i].date == listTest2[a].date)
{
listTest3.Add(listTest2[a]);
}
else
{
listTest3.Add(new Test2() { abbrv = listTest2[a].abbrv, date = listTest2[a].date, completed = true, abbrevName = listTest2[a].abbrevName });
listTest3.Add(new Test2() { abbrv = listTest1[i].abbrv, date = listTest1[i].date, completed = listTest1[i].completed, abbrevName = string.Empty });
}
}
if (!abbrvFound)
{
listTest3.Add(new Test2() { abbrv = listTest1[i].abbrv, date = listTest1[i].date, completed = listTest1[i].completed, abbrevName = string.Empty });
}
}
foreach (var test in listTest3)
{
Console.WriteLine(test.abbrv + " " + test.date + " " + test.completed + " " + test.abbrevName);
}
Console.Write(1);
}
}
public class Test1
{
public string abbrv { get; set; }
public DateTime date { get; set; }
public bool completed { get; set; }
}
public class Test2
{
public string abbrv { get; set; }
public DateTime date { get; set; }
public bool completed { get; set; }
public string abbrevName { get; set; }
}