我正在尝试使用不同的对象在两个列表中进行迭代。如果在比较listTest1
和listTest2
时它们满足以下条件,则会将记录添加到第三个列表中
如果abbrv
和date
相同,则将对象从listTest2
添加到listTest3
如果abbrv
相同但date
不同,则将对象从listTest2
添加到listTest3
,并将完成的属性切换为true
。另外,我将记录从listTest1
添加到listTest3
。
如果abbrv
中的listTest1
中不存在listTest2
,我正在将listTest1
中的记录添加到listTest3
中。
我知道这听起来很令人困惑,这就是为什么我将向您展示我所得到的和我所期望的。
我正进入(状态:
我需要获取所有这些记录,但重复项(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; }
}
最佳答案
您正确实现了前两个条件,但无法在最内部的循环中实现第三个条件:在内部循环结束之前,您无法确定abbrv
中是否存在listTest1
中的listTest2
。
添加一个bool
变量abbrvFound
表示已找到一个abbrv
。进入嵌套循环之前,将其设置为false
;如果找到匹配项,请将其设置为true
。
循环结束后,检查abbrvFound
以确定是否需要添加listTest1
对象。
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 });
}
}