我正在尝试使用不同的对象在两个列表中进行迭代。如果在比较listTest1listTest2时它们满足以下条件,则会将记录添加到第三个列表中


如果abbrvdate相同,则将对象从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 });
    }
}

09-19 02:22