Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#-对象列表中的公共对象_C#_Linq - Fatal编程技术网

c#-对象列表中的公共对象

c#-对象列表中的公共对象,c#,linq,C#,Linq,我有以下个人对象列表 List<List<Person>> setofPersons=new List<Lits<Person>>(); 然后,我将LINQ查询更改为以下内容 List<Person> commonPersons = setofPersons[0].Where( i => setofPersons.Skip(1).Al

我有以下个人对象列表

List<List<Person>> setofPersons=new List<Lits<Person>>();
然后,我将LINQ查询更改为以下内容

              List<Person> commonPersons = setofPersons[0].Where(
                                  i => setofPersons.Skip(1).All(x => i.CompareTo(x))
                              ).ToList();
List commonPersons=setof persons[0]。其中(
i=>setofPersons.Skip(1).All(x=>i.CompareTo(x))
).ToList();
它现在生成一个编译时错误,表示“无法将bool转换为int”。
我是LINQ的新手。有人能指出我哪里出错了吗?

问题是计算机无法知道person类的两个实例是否相同。要告诉.NET运行时如何做到这一点,请使Person类实现IComparable,并在那里指定如何比较Person类的两个实例(在您的示例中,您将检查ID是否相等)。看


更正:您需要实现,Compariable用于排序/排序。

尝试在集合中思考。。。要获得您所拥有的,您需要:

  • 将集合中的所有数据展平到单个person集合中,然后按person.id对它们进行分组
  • 然后根据个人id对其进行分组
  • 最后,只将它们过滤到与原始集合计数相同的组
  • 代码如下:

    var query = setOfPersons
                .SelectMany(personList => personList.Select(person => person))
                .GroupBy(person => person.Id)
                .Where(group => group.Count() == setOfPersons.Count);
    

    请参见此处的最后一个示例:

    您需要做的是将列表列表展平为单个列表。为此,可以使用SelectMany

    代码如下所示:

    var commonPersons = setOfPersons.SelectMany(x => x.Where(y => y.Id == 100));
    

    i可比较
    /
    CompareTo
    用于。您应该做的是重写
    Equals
    GetHashCode
    或实现
    IEqualityComparer

    一个简单的平等实现可以如下所示,例如:

    public override bool Equals(object obj)
    {
        var person = (Person)obj;
        return Equals(Id, person.Id);
    }
    
    public override int GetHashCode()
    {
        return Id;
    }
    
    那么你的普通人可以通过以下方式找到:

    var commonPersons = firstPersonList
        .Intersect(secondPersonList)
        .Intersect(thirdPersonList);
    

    对于多个枚举的交叉点(例如列表列表),有更有效的方法可以做到这一点,如

    的答案所示。我不清楚您想做什么。您想要列表的交叉点吗?您可以使用
    可枚举的.Intersect()
    ?如果没有,你想要什么?请精确显示给定输入所需的输出。是的…我需要列表的交集。我将CompareTo方法添加到我的Person类中。但当我将LINQ查询更改为包含CompareTo方法时,它会生成一个编译时错误,称为“无法将bool转换为int”。我是LINQ的新手。你能指出我哪里做错了吗?IComparable是排序,不是相等。
    var commonPersons = setOfPersons.SelectMany(x => x.Where(y => y.Id == 100));
    
    public override bool Equals(object obj)
    {
        var person = (Person)obj;
        return Equals(Id, person.Id);
    }
    
    public override int GetHashCode()
    {
        return Id;
    }
    
    var commonPersons = firstPersonList
        .Intersect(secondPersonList)
        .Intersect(thirdPersonList);