C# Ef多对多linq查询,该查询获取具有所有列表成员的对象

C# Ef多对多linq查询,该查询获取具有所有列表成员的对象,c#,entity-framework,linq-to-entities,many-to-many,C#,Entity Framework,Linq To Entities,Many To Many,我使用的是实体框架。我有两个领域模型 public class Animal { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Feature> Features { get; set; } } public class Feature { public int Id { get; set; } publi

我使用的是实体框架。我有两个领域模型

public class Animal
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Feature> Features { get; set; }
}   

public class Feature
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Animal> Animals { get; set; }
}
公共类动物
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection功能{get;set;}
}   
公共类功能
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection动物{get;set;}
}
动物和特征具有多对多关系,因此我在DB中有以下表格:

  • 畜生
  • 特征
  • 动物特征
我有一张单子

var featureList = new List<Feature> { new Feature { Name = "f1" }, new Feature { Name = "f2" } };
var-featureList=new-List{new-Feature{Name=“f1”},new-Feature{Name=“f2”};
如何获取包含featureList中所有特征的动物列表


任何帮助都将不胜感激。我在这件事上陷入了困境。

你可以试试这样的东西:

var features =  { "f1","f2" };
var query = from a in dc.Animals
            where ( from f in a.Features
                    where features.Contains(f.Name)
                    select f).Count() == features.Length
            select a;
我建议您使用基本类型集合,而不是
功能的列表
,因为当您需要检查特定集合中是否包含多个元素时,EF仅适用于基本类型和枚举类型。在
where
条件下,无法比较两个自定义对象。请记住,此查询将被转换为sql,而EF不知道如何转换两个对象之间的比较。所以,正如我前面所说的,在这种查询中应该使用基元类型或枚举类型。你可以选择拥有这些动物所需的特征的名称或ID,并像我上面所做的那样与该集合进行比较

无论如何,我认为您也可以使用
Any
扩展方法进行另一个查询:

var featureList = new List<Feature> { new Feature { Name = "f1" }, new Feature { Name = "f2" } };

var query = from a in dc.Animals
            where ( from f in a.Features
                    where featureList.Any(e=>e.Name==f.Name)
                    select f).Count() == featureList.Count
            select a;
var-featureList=new-List{new-Feature{Name=“f1”},new-Feature{Name=“f2”};
var query=来自dc.Animals中的
其中(从f到a.特征)
其中featureList.Any(e=>e.Name==f.Name)
选择f).Count()==featureList.Count
选择一个;

但我特别喜欢第一种变体。

您可以尝试以下方法:

var features =  { "f1","f2" };
var query = from a in dc.Animals
            where ( from f in a.Features
                    where features.Contains(f.Name)
                    select f).Count() == features.Length
            select a;
我建议您使用基本类型集合,而不是
功能的列表
,因为当您需要检查特定集合中是否包含多个元素时,EF仅适用于基本类型和枚举类型。在
where
条件下,无法比较两个自定义对象。请记住,此查询将被转换为sql,而EF不知道如何转换两个对象之间的比较。所以,正如我前面所说的,在这种查询中应该使用基元类型或枚举类型。你可以选择拥有这些动物所需的特征的名称或ID,并像我上面所做的那样与该集合进行比较

无论如何,我认为您也可以使用
Any
扩展方法进行另一个查询:

var featureList = new List<Feature> { new Feature { Name = "f1" }, new Feature { Name = "f2" } };

var query = from a in dc.Animals
            where ( from f in a.Features
                    where featureList.Any(e=>e.Name==f.Name)
                    select f).Count() == featureList.Count
            select a;
var-featureList=new-List{new-Feature{Name=“f1”},new-Feature{Name=“f2”};
var query=来自dc.Animals中的
其中(从f到a.特征)
其中featureList.Any(e=>e.Name==f.Name)
选择f).Count()==featureList.Count
选择一个;

但是我特别喜欢第一个变体。

您在linq查询中包含导航属性了吗?@DevilSuichiro没有。但我看不出这对我有什么帮助。你在linq查询中包含导航属性了吗?@DevilSuichiro没有。但我看不出这对我有什么帮助。