Linq 查找集合属性包含其他列表中项目的所有项目

Linq 查找集合属性包含其他列表中项目的所有项目,linq,entity-framework,Linq,Entity Framework,我有一个对象Foo集合,其ICollection属性包含人员对象列表 public class Foo { public int Id { get; set; } public string Name { get; set; } public ICollection<Person> People { get; set; } } 如果有必要的话,我会将其与实体框架一起使用。您指的是使用C#Linq的方法 Any方法基本上说明该集合(可枚举)中的任何元素是否满足条件,在您的

我有一个对象Foo集合,其ICollection属性包含人员对象列表

public class Foo
{
  public int Id { get; set; }
  public string Name { get; set; }
  public ICollection<Person> People { get; set; }
}
如果有必要的话,我会将其与实体框架一起使用。

您指的是使用C#Linq的方法

Any
方法基本上说明该集合(可枚举)中的任何元素是否满足条件,在您的情况下,条件是另一个集合是否包含其中一个元素


我希望这能让你走上正确的方向。实体框架并不重要,因为它们是可枚举的。几乎忘了提到Linq方法非常简单,因此它们真的不需要在它们自己的方法中使用它们。

我最终实现了一个帮助器方法,如下所示:

    public static bool HasElement<T>(ICollection<T> original, ICollection<T> otherCollection)
    {
        return original.Any(otherCollection.Contains);
    }
公共静态bool元素(ICollection original、ICollection otherCollection)
{
返回原件.Any(otherCollection.Contains);
}

希望有帮助

如果我正确理解LINQ,这将导致O(n²),对吗?
var result = from f in FooCollection
             where f.People.Contains(otherPeople)
             select f;
public bool HasPeople(ICollection<Person> original, ICollection<Person> otherPeople)
{
    return original.Any(p => otherPeople.Contains(p));
}
public IEnumerable<Person> GetPeople(ICollection<Person> original, ICollection<Person> otherPeople)
{
    return original.Where(p => otherPeople.Contains(p));
}
    public static bool HasElement<T>(ICollection<T> original, ICollection<T> otherCollection)
    {
        return original.Any(otherCollection.Contains);
    }