Reputation: 163
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
abbrv
and the date
is the same I am adding the object from listTest2
to listTest3
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
.abbrv
from listTest1
doesn't exist in listTest2
I am adding the record from listTest1
to listTest3
.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:
I need to get all these records but the duplicate(Test2).
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" };
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(test3);
listTest2.Add(test4);
for (int i = 0; i < listTest1.Count; i++)
{
for (int a = 0; a < listTest2.Count; a++)
{
if (listTest1[i].abbrv == listTest2[a].abbrv && listTest1[i].date == listTest2[a].date)
{
if (!listTest3.Any(x => x.abbrv == listTest1[i].abbrv))
{
listTest3.Add(listTest2[a]);
}
}
else
{
if (listTest1[i].abbrv == listTest2[a].abbrv)
{
if (!listTest3.Any(x => x.abbrv == listTest1[i].abbrv && x.date != listTest1[i].date))
{
listTest3.Add(new Test.Test2() { abbrv = listTest2[a].abbrv, date = listTest2[a].date, completed = true, abbrevName = listTest2[a].abbrevName });
listTest3.Add(new Test.Test2() { abbrv = listTest1[i].abbrv, date = listTest1[i].date, completed = listTest1[i].completed, abbrevName = string.Empty });
}
}
else if (listTest1[i].abbrv != listTest2[a].abbrv)
{
if(!listTest3.Any(x => x.abbrv == listTest1[i].abbrv))
{
listTest3.Add(new Test.Test2() { abbrv = listTest1[i].abbrv, date = listTest1[i].date, completed = listTest1[i].completed, abbrevName = string.Empty });
}
}
}
}
}
}
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; }
}
Upvotes: 2
Views: 139
Reputation: 726479
You implemented the first two conditions correctly, but the third condition cannot be implemented in the innermost loop: you cannot tell if abbrv
from listTest1
exists in listTest2
until your inner loop is over.
Add a bool
variable abbrvFound
indicating that an abbrv
is found. Set it to false
before entering the nested loop; if a match is found, set it to true
.
After the loop is over, check abbrvFound
to decide if you need to add listTest1
object or not.
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 Test.Test2() { abbrv = listTest2[a].abbrv, date = listTest2[a].date, completed = true, abbrevName = listTest2[a].abbrevName });
listTest3.Add(new Test.Test2() { abbrv = listTest1[i].abbrv, date = listTest1[i].date, completed = listTest1[i].completed, abbrevName = string.Empty });
}
}
if (!abbrvFound) {
listTest3.Add(new Test.Test2() { abbrv = listTest1[i].abbrv, date = listTest1[i].date, completed = listTest1[i].completed, abbrevName = string.Empty });
}
}
Upvotes: 1