如何比较列表之间的嵌套属性

本文关键字:之间 嵌套 属性 列表 何比较 比较 | 更新日期: 2023-09-27 18:28:27

所以,我基本上是在创建一个类似版本控制的系统。我有以下类别Form:

public class Form
{
    public long Id {get; private set;}
    /* among other things */
}

然后,另一个类似的类:

public class Conflict
{
    List<Form> localForms;
    List<Form> remoteForms;
    /* among other things */
}

然后是维持CCD_ 3 s的CCD_ 2的主类。

public class Main
{
    List<Conflict> conflicts;
    /*
     * more stuff...
    */
    public void AddFormConflict(List<Form> locals, List<Form> remotes)
    {
        ...what goes here?
    }
}

我想确保在即将添加新的Conflict对象时,它不包含重复的数据;换句话说,我想将List<Form> locals参数的Ids与conflicts列表的localForms成员所包含的Ids进行比较。遥控器也是如此。此外,我不仅想知道是否存在这样的匹配,而且我还想得到一个参考

基本上长话短说,我想将对象列表中的属性与另一个结构相似的对象列表中相应的属性进行比较。。。它们由列表中的另一个类所包含。

我敢肯定,一定有一些相对简单的方法可以用大约2-3行的linq来做这样的事情,对吧?我一辈子都不能把我的头裹在所有的层面上!啊。请帮忙?

如何比较列表之间的嵌套属性

使属性公开

public class Conflict
{
    public List<Form> localForms { get; set; }
    public List<Form> remoteForms { get; set; }
    /* among other things */
}

你可以检查像这个一样的重复

public class Main
{
    List<Conflict> conflicts;
    public void AddFormConflict(List<Form> locals, List<Form> remotes)
    {
        if (conflicts.Any(c => c.localForms.Any(lf => locals.Any(lc => lf.Id == lc.Id))))
        {
            //duplicate found for localForms
        }
        //similarly check for remoteForms
    }
}